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 pathregistrymux_test.go
More file actions
147 lines (128 loc) · 3.92 KB
/
registrymux_test.go
File metadata and controls
147 lines (128 loc) · 3.92 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
138
139
140
141
142
143
144
145
146
147
// Copyright 2016 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 rpc
import (
"io/ioutil"
"os"
"testing"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg/lease"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/systemd"
"github.com/coreos/fleet/unit"
)
type action struct {
key string
val string
rec bool
}
type testEtcdKeysAPI struct {
etcd.KeysAPI
gets []action
sets []action
deletes []action
res []*etcd.Response // errors returned from subsequent calls to etcd
ri int
err []error // results returned from subsequent calls to etcd
ei int
}
func (t *testEtcdKeysAPI) Set(_ context.Context, key string, value string, _ *etcd.SetOptions) (*etcd.Response, error) {
t.sets = append(t.sets, action{key: key, val: value})
return t.next()
}
func (t *testEtcdKeysAPI) Get(_ context.Context, key string, opts *etcd.GetOptions) (*etcd.Response, error) {
act := action{key: key}
if opts != nil {
act.rec = opts.Recursive
}
t.gets = append(t.gets, act)
return t.next()
}
func (t *testEtcdKeysAPI) Delete(_ context.Context, key string, opts *etcd.DeleteOptions) (*etcd.Response, error) {
act := action{key: key}
if opts != nil {
act.rec = opts.Recursive
}
t.deletes = append(t.deletes, act)
return t.next()
}
func (t *testEtcdKeysAPI) next() (r *etcd.Response, e error) {
if t.ri < len(t.res) {
r = t.res[t.ri]
t.ri++
}
if t.ei < len(t.err) {
e = t.err[t.ei]
t.ei++
}
return r, e
}
func TestRegistryMuxUnitManagement(t *testing.T) {
uDir, err := ioutil.TempDir("", "fleet-")
if err != nil {
t.Fatalf("failed creating tempdir: %v", err)
}
defer os.RemoveAll(uDir)
state := &machine.MachineState{
ID: "id",
PublicIP: "127.0.0.1",
Metadata: make(map[string]string, 0),
}
mgr, err := systemd.NewSystemdUnitManager(uDir, false, false)
if err != nil {
// NOTE: ideally we should fail with t.Fatalf(), but then it would always
// fail on travis CI, because apparently systemd dbus socket is not
// available there. So let's just skip the test for now.
// In the long run, we should find a way to test it correctly on travis.
// - dpark 20160812
t.Skipf("unexpected error creating systemd unit manager: %v", err)
}
mach := machine.NewCoreOSMachine(*state, mgr)
e := &testEtcdKeysAPI{}
etcdReg := registry.NewEtcdRegistry(e, "/fleet/")
lManager := lease.NewEtcdLeaseManager(e, "/fleet/")
reg := NewRegistryMux(etcdReg, mach, lManager)
contents := `
[Unit]
Description = Foo
`
unitFile, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("unexpected error parsing unit %q: %v", contents, err)
}
unit := &job.Unit{
Name: "foo",
Unit: *unitFile,
TargetState: job.JobStateLoaded,
}
if err := reg.CreateUnit(unit); err != nil {
t.Fatalf("unexpected error creating an unit: %v", err)
}
machineID := "testMachine"
if err := reg.ScheduleUnit(unit.Name, machineID); err != nil {
t.Fatalf("unexpected error scheduling an unit: %v", err)
}
if err := reg.UnscheduleUnit(unit.Name, machineID); err != nil {
t.Fatalf("unexpected error unscheduling an unit: %v", err)
}
if err := reg.DestroyUnit(unit.Name); err != nil {
t.Fatalf("unexpected error destroying an unit: %v", err)
}
if err := reg.RemoveMachineState(machineID); err != nil {
t.Fatalf("unexpected error removing machine state: %v", err)
}
}