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