-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathmodelsfile_test.go
140 lines (121 loc) · 3.56 KB
/
modelsfile_test.go
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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient_test
import (
"os"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/core/model"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/juju/osenv"
"github.com/juju/juju/jujuclient"
)
type ModelsFileSuite struct {
testing.FakeJujuXDGDataHomeSuite
}
var _ = gc.Suite(&ModelsFileSuite{})
const testModelsYAML = `
controllers:
ctrl:
models:
admin/admin:
uuid: ghi
type: iaas
kontroll:
models:
admin/admin:
uuid: abc
type: iaas
admin/my-model:
uuid: def
type: iaas
current-model: admin/my-model
`
const testLegacyModelsYAML = `
controllers:
ctrl:
models:
admin@local/admin:
uuid: ghi
kontroll:
models:
admin@local/admin:
uuid: abc
admin@local/my-model:
uuid: def
current-model: admin@local/my-model
`
var testControllerModels = map[string]*jujuclient.ControllerModels{
"kontroll": {
Models: map[string]jujuclient.ModelDetails{
"admin/admin": kontrollAdminModelDetails,
"admin/my-model": kontrollMyModelModelDetails,
},
CurrentModel: "admin/my-model",
},
"ctrl": {
Models: map[string]jujuclient.ModelDetails{
"admin/admin": ctrlAdminModelDetails,
},
},
}
var kontrollAdminModelDetails = jujuclient.ModelDetails{
ModelUUID: "abc",
ModelType: model.IAAS,
}
var kontrollMyModelModelDetails = jujuclient.ModelDetails{
ModelUUID: "def",
ModelType: model.IAAS,
}
var ctrlAdminModelDetails = jujuclient.ModelDetails{
ModelUUID: "ghi",
ModelType: model.IAAS,
}
func (s *ModelsFileSuite) TestWriteFile(c *gc.C) {
writeTestModelsFile(c)
data, err := os.ReadFile(osenv.JujuXDGDataHomePath("models.yaml"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, testModelsYAML[1:])
}
func (s *ModelsFileSuite) TestReadNoFile(c *gc.C) {
models, err := jujuclient.ReadModelsFile("nowhere.yaml")
c.Assert(err, jc.ErrorIsNil)
c.Assert(models, gc.IsNil)
}
func (s *ModelsFileSuite) TestReadEmptyFile(c *gc.C) {
err := os.WriteFile(osenv.JujuXDGDataHomePath("models.yaml"), []byte(""), 0600)
c.Assert(err, jc.ErrorIsNil)
models, err := jujuclient.ReadModelsFile(jujuclient.JujuModelsPath())
c.Assert(err, jc.ErrorIsNil)
c.Assert(models, gc.HasLen, 0)
}
func (s *ModelsFileSuite) TestMigrateLegacyLocal(c *gc.C) {
err := os.WriteFile(jujuclient.JujuModelsPath(), []byte(testLegacyModelsYAML), 0644)
c.Assert(err, jc.ErrorIsNil)
models, err := jujuclient.ReadModelsFile(jujuclient.JujuModelsPath())
c.Assert(err, jc.ErrorIsNil)
migratedData, err := os.ReadFile(jujuclient.JujuModelsPath())
c.Assert(err, jc.ErrorIsNil)
migratedModels, err := jujuclient.ParseModels(migratedData)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(migratedData), jc.DeepEquals, testModelsYAML[1:])
c.Assert(migratedModels, jc.DeepEquals, models)
}
func writeTestModelsFile(c *gc.C) {
err := jujuclient.WriteModelsFile(testControllerModels)
c.Assert(err, jc.ErrorIsNil)
}
func (s *ModelsFileSuite) TestParseModels(c *gc.C) {
models, err := jujuclient.ParseModels([]byte(testModelsYAML))
c.Assert(err, jc.ErrorIsNil)
c.Assert(models, jc.DeepEquals, testControllerModels)
}
func (s *ModelsFileSuite) TestParseModelMetadataError(c *gc.C) {
models, err := jujuclient.ParseModels([]byte("fail me now"))
c.Assert(err, gc.ErrorMatches,
"cannot unmarshal models: yaml: unmarshal errors:"+
"\n line 1: cannot unmarshal !!str `fail me...` into "+
"jujuclient.modelsCollection",
)
c.Assert(models, gc.IsNil)
}