|
| 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 | + "io/ioutil" |
| 19 | + "net" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +// TestSdNotify |
| 25 | +func TestSdNotify(t *testing.T) { |
| 26 | + notificationSupportedDataSent := "Notification supported, data sent" |
| 27 | + notificationSupportedFailure := "Notification supported, but failure happened" |
| 28 | + notificationNotSupported := "Notification not supported" |
| 29 | + |
| 30 | + testDir, e := ioutil.TempDir("/tmp/", "test-") |
| 31 | + if e != nil { |
| 32 | + panic(e) |
| 33 | + } |
| 34 | + defer os.RemoveAll(testDir) |
| 35 | + |
| 36 | + notifySocket := testDir + "/notify-socket.sock" |
| 37 | + laddr := net.UnixAddr{ |
| 38 | + Name: notifySocket, |
| 39 | + Net: "unixgram", |
| 40 | + } |
| 41 | + _, e = net.ListenUnixgram("unixgram", &laddr) |
| 42 | + if e != nil { |
| 43 | + panic(e) |
| 44 | + } |
| 45 | + |
| 46 | + // (true, nil) - notification supported, data has been sent |
| 47 | + e = os.Setenv("NOTIFY_SOCKET", notifySocket) |
| 48 | + if e != nil { |
| 49 | + panic(e) |
| 50 | + } |
| 51 | + sent, err := SdNotify(notificationSupportedDataSent) |
| 52 | + if !sent || err != nil { |
| 53 | + t.Errorf("TEST: %s FAILED", notificationSupportedDataSent) |
| 54 | + } |
| 55 | + |
| 56 | + // (false, err) - notification supported, but failure happened |
| 57 | + e = os.Setenv("NOTIFY_SOCKET", testDir+"/not-exist.sock") |
| 58 | + if e != nil { |
| 59 | + panic(e) |
| 60 | + } |
| 61 | + sent, err = SdNotify(notificationSupportedFailure) |
| 62 | + if sent && err == nil { |
| 63 | + t.Errorf("TEST: %s FAILED", notificationSupportedFailure) |
| 64 | + } |
| 65 | + |
| 66 | + // (false, nil) - notification not supported |
| 67 | + e = os.Unsetenv("NOTIFY_SOCKET") |
| 68 | + if e != nil { |
| 69 | + panic(e) |
| 70 | + } |
| 71 | + sent, err = SdNotify(notificationNotSupported) |
| 72 | + if sent || err != nil { |
| 73 | + t.Errorf("TEST: %s FAILED", notificationNotSupported) |
| 74 | + } |
| 75 | +} |
0 commit comments