This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathsystemd_test.go
More file actions
137 lines (115 loc) · 3.17 KB
/
systemd_test.go
File metadata and controls
137 lines (115 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014 The fleet Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package functional
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"reflect"
"testing"
"time"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/systemd"
"github.com/coreos/fleet/unit"
)
func TestSystemdUnitFlow(t *testing.T) {
uDir, err := ioutil.TempDir("", "fleet-")
if err != nil {
t.Fatalf("Failed creating tempdir: %v", err)
}
defer os.RemoveAll(uDir)
mgr, err := systemd.NewSystemdUnitManager(uDir, false)
if err != nil {
t.Fatalf("Failed initializing SystemdUnitManager: %v", err)
}
units, err := mgr.Units()
if err != nil {
t.Fatalf("Failed calling Units(): %v", err)
}
if len(units) > 0 {
t.Fatalf("Expected no units to be returned, got %v", units)
}
contents := `[Service]
ExecStart=/usr/bin/sleep 3000
`
name := fmt.Sprintf("fleet-unit-%d.service", rand.Int63())
uf, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("Invalid unit file: %v", err)
}
hash := uf.Hash().String()
j := job.NewJob(name, *uf)
if err := mgr.Load(j.Name, j.Unit); err != nil {
t.Fatalf("Failed loading job: %v", err)
}
units, err = mgr.Units()
if err != nil {
t.Fatalf("Failed calling Units(): %v", err)
}
if !reflect.DeepEqual([]string{name}, units) {
t.Fatalf("Expected [hello.service], got %v", units)
}
err = waitForUnitState(mgr, name, unit.UnitState{"loaded", "inactive", "dead", "", hash, "", 0})
if err != nil {
t.Error(err)
}
err = mgr.TriggerStart(name)
if err != nil {
t.Error(err)
}
err = waitForUnitState(mgr, name, unit.UnitState{"loaded", "active", "running", "", hash, "", 0})
if err != nil {
t.Error(err)
}
err = mgr.TriggerStop(name)
if err != nil {
t.Error(err)
}
mgr.Unload(name)
units, err = mgr.Units()
if err != nil {
t.Fatalf("Failed calling Units(): %v", err)
}
if len(units) > 0 {
t.Fatalf("Expected no units to be returned, got %v", units)
}
}
func waitForUnitState(mgr unit.UnitManager, name string, want unit.UnitState) error {
timeout := time.After(time.Second)
for {
select {
case <-timeout:
return fmt.Errorf("Timed out waiting for state of %s to match %#v", name, want)
default:
}
got, err := mgr.GetUnitState(name)
if err != nil {
return err
}
if isEqualUnitState(want, *got) {
return nil
}
}
}
// isEqualUnitState checks if both units are the same,
// excluding ActiveEnterTimestamp field of each unit state.
func isEqualUnitState(src, dst unit.UnitState) bool {
return src.LoadState == dst.LoadState &&
src.ActiveState == dst.ActiveState &&
src.SubState == dst.SubState &&
src.MachineID == dst.MachineID &&
src.UnitHash == dst.UnitHash &&
src.UnitName == dst.UnitName
}