|
| 1 | +package campaigns |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v2" |
| 8 | +) |
| 9 | + |
| 10 | +const testExecutionCacheKeyEnv = "TEST_EXECUTION_CACHE_KEY_ENV" |
| 11 | + |
| 12 | +func TestExecutionCacheKey(t *testing.T) { |
| 13 | + // Let's set up an array of steps that we can test with. One step will |
| 14 | + // depend on an environment variable outside the spec. |
| 15 | + var steps []Step |
| 16 | + if err := yaml.Unmarshal([]byte(` |
| 17 | +- run: foo |
| 18 | + env: |
| 19 | + FOO: BAR |
| 20 | +
|
| 21 | +- run: bar |
| 22 | + env: |
| 23 | + - FOO: BAR |
| 24 | + - `+testExecutionCacheKeyEnv+` |
| 25 | +`), &steps); err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + // And now we can set up a key to work with. |
| 30 | + key := ExecutionCacheKey{&Task{Steps: steps}} |
| 31 | + |
| 32 | + // All righty. Let's get ourselves a baseline cache key here. |
| 33 | + initial, err := key.Key() |
| 34 | + if err != nil { |
| 35 | + t.Errorf("unexpected error: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + // Let's set an unrelated environment variable and ensure we still have the |
| 39 | + // same key. |
| 40 | + if err := os.Setenv(testExecutionCacheKeyEnv+"_UNRELATED", "foo"); err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + have, err := key.Key() |
| 44 | + if err != nil { |
| 45 | + t.Errorf("unexpected error: %v", err) |
| 46 | + } |
| 47 | + if string(initial) != string(have) { |
| 48 | + t.Errorf("unexpected change in key: initial=%q have=%q", initial, have) |
| 49 | + } |
| 50 | + |
| 51 | + // Let's now set the environment variable referenced in the steps and verify |
| 52 | + // that the cache key does change. |
| 53 | + if err := os.Setenv(testExecutionCacheKeyEnv, "foo"); err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + have, err = key.Key() |
| 57 | + if err != nil { |
| 58 | + t.Errorf("unexpected error: %v", err) |
| 59 | + } |
| 60 | + if string(initial) == string(have) { |
| 61 | + t.Errorf("unexpected lack of change in key: %q", have) |
| 62 | + } |
| 63 | + |
| 64 | + // And, just to be sure, let's change it again. |
| 65 | + if err := os.Setenv(testExecutionCacheKeyEnv, "bar"); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + again, err := key.Key() |
| 69 | + if err != nil { |
| 70 | + t.Errorf("unexpected error: %v", err) |
| 71 | + } |
| 72 | + if string(initial) == string(again) || string(have) == string(again) { |
| 73 | + t.Errorf("unexpected lack of change in key: %q", again) |
| 74 | + } |
| 75 | + |
| 76 | + // Finally, if we unset the environment variable again, we should get a key |
| 77 | + // that matches the initial key. |
| 78 | + if err := os.Unsetenv(testExecutionCacheKeyEnv); err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + have, err = key.Key() |
| 82 | + if err != nil { |
| 83 | + t.Errorf("unexpected error: %v", err) |
| 84 | + } |
| 85 | + if string(initial) != string(have) { |
| 86 | + t.Errorf("unexpected change in key: initial=%q have=%q", initial, have) |
| 87 | + } |
| 88 | +} |
0 commit comments