github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/nomad/resource_job_test.go (about) 1 package nomad 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "testing" 8 9 r "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 12 "github.com/hashicorp/nomad/api" 13 ) 14 15 func TestResourceJob_basic(t *testing.T) { 16 r.Test(t, r.TestCase{ 17 Providers: testProviders, 18 PreCheck: func() { testAccPreCheck(t) }, 19 Steps: []r.TestStep{ 20 { 21 Config: testResourceJob_initialConfig, 22 Check: testResourceJob_initialCheck, 23 }, 24 }, 25 26 CheckDestroy: testResourceJob_checkDestroy("foo"), 27 }) 28 } 29 30 func TestResourceJob_refresh(t *testing.T) { 31 r.Test(t, r.TestCase{ 32 Providers: testProviders, 33 PreCheck: func() { testAccPreCheck(t) }, 34 Steps: []r.TestStep{ 35 { 36 Config: testResourceJob_initialConfig, 37 Check: testResourceJob_initialCheck, 38 }, 39 40 // This should successfully cause the job to be recreated, 41 // testing the Exists function. 42 { 43 PreConfig: testResourceJob_deregister(t, "foo"), 44 Config: testResourceJob_initialConfig, 45 }, 46 }, 47 }) 48 } 49 50 func TestResourceJob_disableDestroyDeregister(t *testing.T) { 51 r.Test(t, r.TestCase{ 52 Providers: testProviders, 53 PreCheck: func() { testAccPreCheck(t) }, 54 Steps: []r.TestStep{ 55 { 56 Config: testResourceJob_noDestroy, 57 Check: testResourceJob_initialCheck, 58 }, 59 60 // Destroy with our setting set 61 { 62 Destroy: true, 63 Config: testResourceJob_noDestroy, 64 Check: testResourceJob_checkExists, 65 }, 66 67 // Re-apply without the setting set 68 { 69 Config: testResourceJob_initialConfig, 70 Check: testResourceJob_checkExists, 71 }, 72 }, 73 }) 74 } 75 76 func TestResourceJob_idChange(t *testing.T) { 77 r.Test(t, r.TestCase{ 78 Providers: testProviders, 79 PreCheck: func() { testAccPreCheck(t) }, 80 Steps: []r.TestStep{ 81 { 82 Config: testResourceJob_initialConfig, 83 Check: testResourceJob_initialCheck, 84 }, 85 86 // Change our ID 87 { 88 Config: testResourceJob_updateConfig, 89 Check: testResourceJob_updateCheck, 90 }, 91 }, 92 }) 93 } 94 95 func TestResourceJob_parameterizedJob(t *testing.T) { 96 r.Test(t, r.TestCase{ 97 Providers: testProviders, 98 PreCheck: func() { testAccPreCheck(t) }, 99 Steps: []r.TestStep{ 100 { 101 Config: testResourceJob_parameterizedJob, 102 Check: testResourceJob_initialCheck, 103 }, 104 }, 105 }) 106 } 107 108 var testResourceJob_initialConfig = ` 109 resource "nomad_job" "test" { 110 jobspec = <<EOT 111 job "foo" { 112 datacenters = ["dc1"] 113 type = "service" 114 group "foo" { 115 task "foo" { 116 driver = "raw_exec" 117 config { 118 command = "/bin/sleep" 119 args = ["1"] 120 } 121 122 resources { 123 cpu = 20 124 memory = 10 125 } 126 127 logs { 128 max_files = 3 129 max_file_size = 10 130 } 131 } 132 } 133 } 134 EOT 135 } 136 ` 137 138 var testResourceJob_noDestroy = ` 139 resource "nomad_job" "test" { 140 deregister_on_destroy = false 141 jobspec = <<EOT 142 job "foo" { 143 datacenters = ["dc1"] 144 type = "service" 145 group "foo" { 146 task "foo" { 147 driver = "raw_exec" 148 config { 149 command = "/bin/sleep" 150 args = ["1"] 151 } 152 153 resources { 154 cpu = 20 155 memory = 10 156 } 157 158 logs { 159 max_files = 3 160 max_file_size = 10 161 } 162 } 163 } 164 } 165 EOT 166 } 167 ` 168 169 func testResourceJob_initialCheck(s *terraform.State) error { 170 resourceState := s.Modules[0].Resources["nomad_job.test"] 171 if resourceState == nil { 172 return errors.New("resource not found in state") 173 } 174 175 instanceState := resourceState.Primary 176 if instanceState == nil { 177 return errors.New("resource has no primary instance") 178 } 179 180 jobID := instanceState.ID 181 182 client := testProvider.Meta().(*api.Client) 183 job, _, err := client.Jobs().Info(jobID, nil) 184 if err != nil { 185 return fmt.Errorf("error reading back job: %s", err) 186 } 187 188 if got, want := job.ID, jobID; got != want { 189 return fmt.Errorf("jobID is %q; want %q", got, want) 190 } 191 192 return nil 193 } 194 195 func testResourceJob_checkExists(s *terraform.State) error { 196 jobID := "foo" 197 198 client := testProvider.Meta().(*api.Client) 199 _, _, err := client.Jobs().Info(jobID, nil) 200 if err != nil { 201 return fmt.Errorf("error reading back job: %s", err) 202 } 203 204 return nil 205 } 206 207 func testResourceJob_checkDestroy(jobID string) r.TestCheckFunc { 208 return func(*terraform.State) error { 209 client := testProvider.Meta().(*api.Client) 210 _, _, err := client.Jobs().Info(jobID, nil) 211 if err != nil && strings.Contains(err.Error(), "404") { 212 return nil 213 } 214 if err == nil { 215 err = errors.New("not destroyed") 216 } 217 218 return err 219 } 220 } 221 222 func testResourceJob_deregister(t *testing.T, jobID string) func() { 223 return func() { 224 client := testProvider.Meta().(*api.Client) 225 _, _, err := client.Jobs().Deregister(jobID, nil) 226 if err != nil { 227 t.Fatalf("error deregistering job: %s", err) 228 } 229 } 230 } 231 232 var testResourceJob_updateConfig = ` 233 resource "nomad_job" "test" { 234 jobspec = <<EOT 235 job "bar" { 236 datacenters = ["dc1"] 237 type = "service" 238 group "foo" { 239 task "foo" { 240 driver = "raw_exec" 241 config { 242 command = "/bin/sleep" 243 args = ["1"] 244 } 245 246 resources { 247 cpu = 20 248 memory = 10 249 } 250 251 logs { 252 max_files = 3 253 max_file_size = 10 254 } 255 } 256 } 257 } 258 EOT 259 } 260 ` 261 262 func testResourceJob_updateCheck(s *terraform.State) error { 263 resourceState := s.Modules[0].Resources["nomad_job.test"] 264 if resourceState == nil { 265 return errors.New("resource not found in state") 266 } 267 268 instanceState := resourceState.Primary 269 if instanceState == nil { 270 return errors.New("resource has no primary instance") 271 } 272 273 jobID := instanceState.ID 274 275 client := testProvider.Meta().(*api.Client) 276 job, _, err := client.Jobs().Info(jobID, nil) 277 if err != nil { 278 return fmt.Errorf("error reading back job: %s", err) 279 } 280 281 if got, want := job.ID, jobID; got != want { 282 return fmt.Errorf("jobID is %q; want %q", got, want) 283 } 284 285 { 286 // Verify foo doesn't exist 287 _, _, err := client.Jobs().Info("foo", nil) 288 if err == nil { 289 return errors.New("reading foo success") 290 } 291 } 292 293 return nil 294 } 295 296 var testResourceJob_parameterizedJob = ` 297 resource "nomad_job" "test" { 298 jobspec = <<EOT 299 job "bar" { 300 datacenters = ["dc1"] 301 type = "batch" 302 parameterized { 303 payload = "required" 304 } 305 group "foo" { 306 task "foo" { 307 driver = "raw_exec" 308 config { 309 command = "/bin/sleep" 310 args = ["1"] 311 } 312 resources { 313 cpu = 20 314 memory = 10 315 } 316 317 logs { 318 max_files = 3 319 max_file_size = 10 320 } 321 } 322 } 323 } 324 EOT 325 } 326 `