github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/validator/distro_validator_test.go (about) 1 package validator 2 3 import ( 4 "testing" 5 6 "github.com/evergreen-ci/evergreen/cloud/providers/ec2" 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/model/distro" 9 _ "github.com/evergreen-ci/evergreen/plugin/config" 10 "github.com/evergreen-ci/evergreen/testutil" 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 var conf = testutil.TestConfig() 15 16 func init() { 17 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(conf)) 18 } 19 20 func TestCheckDistro(t *testing.T) { 21 Convey("When validating a distro", t, func() { 22 23 Convey("if a new distro passes all of the validation tests, no errors should be returned", func() { 24 d := &distro.Distro{Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", 25 Provider: ec2.OnDemandProviderName, 26 ProviderSettings: &map[string]interface{}{ 27 "ami": "a", 28 "key_name": "a", 29 "instance_type": "a", 30 "security_group": "a", 31 "mount_points": nil, 32 }, 33 } 34 verrs, err := CheckDistro(d, conf, true) 35 So(err, ShouldBeNil) 36 So(verrs, ShouldResemble, []ValidationError{}) 37 }) 38 39 Convey("if a new distro fails a validation test, an error should be returned", func() { 40 d := &distro.Distro{Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", 41 Provider: ec2.OnDemandProviderName, 42 ProviderSettings: &map[string]interface{}{ 43 "ami": "a", 44 "key_name": "a", 45 "instance_type": "a", 46 "security_group": "a", 47 "mount_points": nil, 48 }, 49 } 50 // simulate duplicate id 51 dupe := distro.Distro{Id: "a"} 52 So(dupe.Insert(), ShouldBeNil) 53 verrs, err := CheckDistro(d, conf, true) 54 So(err, ShouldBeNil) 55 So(verrs, ShouldNotResemble, []ValidationError{}) 56 }) 57 58 Convey("if an existing distro passes all of the validation tests, no errors should be returned", func() { 59 d := &distro.Distro{Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", 60 Provider: ec2.OnDemandProviderName, 61 ProviderSettings: &map[string]interface{}{ 62 "ami": "a", 63 "key_name": "a", 64 "instance_type": "a", 65 "security_group": "a", 66 "mount_points": nil, 67 }, 68 } 69 verrs, err := CheckDistro(d, conf, false) 70 So(err, ShouldBeNil) 71 So(verrs, ShouldResemble, []ValidationError{}) 72 }) 73 74 Convey("if an existing distro fails a validation test, an error should be returned", func() { 75 d := &distro.Distro{Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", 76 Provider: ec2.OnDemandProviderName, 77 ProviderSettings: &map[string]interface{}{ 78 "ami": "", 79 "key_name": "a", 80 "instance_type": "a", 81 "security_group": "a", 82 "mount_points": nil, 83 }, 84 } 85 verrs, err := CheckDistro(d, conf, false) 86 So(err, ShouldBeNil) 87 So(verrs, ShouldNotResemble, []ValidationError{}) 88 // empty ami for provider 89 }) 90 91 Reset(func() { 92 So(db.Clear(distro.Collection), ShouldBeNil) 93 }) 94 }) 95 } 96 97 func TestEnsureUniqueId(t *testing.T) { 98 99 Convey("When validating a distros' ids...", t, func() { 100 distroIds := []string{"a", "b", "c"} 101 Convey("if a distro has a duplicate id, an error should be returned", func() { 102 err := ensureUniqueId(&distro.Distro{Id: "c"}, distroIds) 103 So(err, ShouldNotResemble, []ValidationError{}) 104 So(len(err), ShouldEqual, 1) 105 }) 106 Convey("if a distro doesn't have a duplicate id, no error should be returned", func() { 107 err := ensureUniqueId(&distro.Distro{Id: "d"}, distroIds) 108 So(err, ShouldBeNil) 109 }) 110 }) 111 } 112 113 func TestEnsureHasRequiredFields(t *testing.T) { 114 i := -1 115 Convey("When validating a distro...", t, func() { 116 d := []distro.Distro{ 117 {}, 118 {Id: "a"}, 119 {Id: "a", Arch: "a"}, 120 {Id: "a", Arch: "a", User: "a"}, 121 {Id: "a", Arch: "a", User: "a", SSHKey: "a"}, 122 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a"}, 123 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: "a"}, 124 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName}, 125 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName, ProviderSettings: &map[string]interface{}{ 126 "instance_type": "a", 127 "security_group": "a", 128 "key_name": "a", 129 "mount_points": nil, 130 }}, 131 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName, ProviderSettings: &map[string]interface{}{ 132 "ami": "a", 133 "security_group": "a", 134 "key_name": "a", 135 "mount_points": nil, 136 }}, 137 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName, ProviderSettings: &map[string]interface{}{ 138 "ami": "a", 139 "instance_type": "a", 140 "key_name": "a", 141 "mount_points": nil, 142 }}, 143 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName, ProviderSettings: &map[string]interface{}{ 144 "ami": "a", 145 "instance_type": "a", 146 "security_group": "a", 147 "mount_points": nil, 148 }}, 149 {Id: "a", Arch: "a", User: "a", SSHKey: "a", WorkDir: "a", Provider: ec2.OnDemandProviderName, ProviderSettings: &map[string]interface{}{ 150 "ami": "a", 151 "key_name": "a", 152 "instance_type": "a", 153 "security_group": "a", 154 "mount_points": nil, 155 }}, 156 } 157 i++ 158 Convey("an error should be returned if the distro does not contain an id", func() { 159 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 160 }) 161 Convey("an error should be returned if the distro does not contain an architecture", func() { 162 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 163 }) 164 Convey("an error should be returned if the distro does not contain a user", func() { 165 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 166 }) 167 Convey("an error should be returned if the distro does not contain an ssh key", func() { 168 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 169 }) 170 Convey("an error should be returned if the distro does not contain a working directory", func() { 171 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 172 }) 173 Convey("an error should be returned if the distro does not contain a provider", func() { 174 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 175 }) 176 Convey("an error should be returned if the distro does not contain a valid provider", func() { 177 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 178 }) 179 Convey("an error should be returned if the distro does not contain any provider settings", func() { 180 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 181 }) 182 Convey("an error should be returned if the distro does not contain all required provider settings", func() { 183 Convey("for ec2, it must have the ami", func() { 184 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 185 }) 186 Convey("for ec2, it must have the instance_type", func() { 187 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 188 }) 189 Convey("for ec2, it must have the security group", func() { 190 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 191 }) 192 Convey("for ec2, it must have the key name", func() { 193 So(ensureHasRequiredFields(&d[i], conf), ShouldNotResemble, []ValidationError{}) 194 }) 195 }) 196 Convey("no error should be returned if the distro contains all required provider settings", func() { 197 So(ensureHasRequiredFields(&d[i], conf), ShouldResemble, []ValidationError{}) 198 }) 199 }) 200 } 201 202 func TestEnsureValidExpansions(t *testing.T) { 203 Convey("When validating a distro's expansions...", t, func() { 204 Convey("if any key is blank, an error should be returned", func() { 205 d := &distro.Distro{ 206 Expansions: []distro.Expansion{{"", "b"}, {"c", "d"}}, 207 } 208 err := ensureValidExpansions(d, conf) 209 So(err, ShouldNotResemble, []ValidationError{}) 210 So(len(err), ShouldEqual, 1) 211 }) 212 Convey("if no expansion key is blank, no error should be returned", func() { 213 d := &distro.Distro{ 214 Expansions: []distro.Expansion{{"a", "b"}, {"c", "d"}}, 215 } 216 err := ensureValidExpansions(d, conf) 217 So(err, ShouldBeNil) 218 }) 219 }) 220 } 221 222 func TestEnsureValidSSHOptions(t *testing.T) { 223 Convey("When validating a distro's SSH options...", t, func() { 224 Convey("if any option is blank, an error should be returned", func() { 225 d := &distro.Distro{ 226 SSHOptions: []string{"", "b", "", "d"}, 227 } 228 err := ensureValidSSHOptions(d, conf) 229 So(err, ShouldNotResemble, []ValidationError{}) 230 So(len(err), ShouldEqual, 1) 231 }) 232 Convey("if no option is blank, no error should be returned", func() { 233 d := &distro.Distro{ 234 SSHOptions: []string{"a", "b"}, 235 } 236 err := ensureValidSSHOptions(d, conf) 237 So(err, ShouldBeNil) 238 }) 239 }) 240 }