|
| 1 | +// Copyright 2016 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package daemon |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "strconv" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSdWatchdogEnabled(t *testing.T) { |
| 24 | + // (time, nil) |
| 25 | + err := os.Setenv("WATCHDOG_USEC", "100") |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | + err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid())) |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + |
| 34 | + delay, err := SdWatchdogEnabled() |
| 35 | + if delay == 0 || err != nil { |
| 36 | + t.Errorf("TEST: Watchdog enabled FAILED") |
| 37 | + } |
| 38 | + |
| 39 | + // (0, nil) PID doesnt match |
| 40 | + err = os.Setenv("WATCHDOG_USEC", "100") |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + err = os.Setenv("WATCHDOG_PID", "0") |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + delay, err = SdWatchdogEnabled() |
| 49 | + if delay != 0 || err != nil { |
| 50 | + t.Errorf("TEST: PID doesn't match FAILED") |
| 51 | + } |
| 52 | + |
| 53 | + // (0, nil) WATCHDOG_USEC doen't exist |
| 54 | + err = os.Unsetenv("WATCHDOG_USEC") |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid())) |
| 59 | + if err != nil { |
| 60 | + panic(err) |
| 61 | + } |
| 62 | + delay, err = SdWatchdogEnabled() |
| 63 | + if delay != 0 || err != nil { |
| 64 | + t.Errorf("TEST: WATCHDOG_USEC doen't exist FAILED") |
| 65 | + } |
| 66 | + |
| 67 | + // (0, nil) WATCHDOG_PID doen't exist |
| 68 | + err = os.Setenv("WATCHDOG_USEC", "1") |
| 69 | + if err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + err = os.Unsetenv("WATCHDOG_PID") |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + delay, err = SdWatchdogEnabled() |
| 77 | + if delay != 0 || err != nil { |
| 78 | + t.Errorf("TEST: WATCHDOG_PID doen't exist FAILED") |
| 79 | + } |
| 80 | + |
| 81 | + // (0, err) USEC negative |
| 82 | + err = os.Setenv("WATCHDOG_USEC", "-1") |
| 83 | + if err != nil { |
| 84 | + panic(err) |
| 85 | + } |
| 86 | + err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid())) |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + _, err = SdWatchdogEnabled() |
| 91 | + if err == nil { |
| 92 | + t.Errorf("TEST: USEC negative FAILED") |
| 93 | + } |
| 94 | + |
| 95 | + // (0, err) Bad USEC |
| 96 | + err = os.Setenv("WATCHDOG_USEC", "string") |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + err = os.Setenv("WATCHDOG_PID", strconv.Itoa(os.Getpid())) |
| 101 | + if err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | + _, err = SdWatchdogEnabled() |
| 105 | + if err == nil { |
| 106 | + t.Errorf("TEST: Bad USEC FAILED") |
| 107 | + } |
| 108 | + |
| 109 | + // (0, err) Bad PID |
| 110 | + err = os.Setenv("WATCHDOG_USEC", "1") |
| 111 | + if err != nil { |
| 112 | + panic(err) |
| 113 | + } |
| 114 | + err = os.Setenv("WATCHDOG_PID", "string") |
| 115 | + if err != nil { |
| 116 | + panic(err) |
| 117 | + } |
| 118 | + _, err = SdWatchdogEnabled() |
| 119 | + if err == nil { |
| 120 | + t.Errorf("TEST: Bad PID FAILED") |
| 121 | + } |
| 122 | +} |
0 commit comments