github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/cache_test.go (about) 1 /* 2 Copyright 2019 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package integration 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "path" 24 "testing" 25 "time" 26 27 "github.com/GoogleContainerTools/skaffold/v2/integration/skaffold" 28 "github.com/GoogleContainerTools/skaffold/v2/proto/v1" 29 "github.com/GoogleContainerTools/skaffold/v2/testutil" 30 ) 31 32 func TestCacheAPITriggers(t *testing.T) { 33 MarkIntegrationTest(t, CanRunWithoutGcp) 34 35 // Run skaffold build first to fail quickly on a build failure 36 skaffold.Build().InDir("examples/getting-started").RunOrFail(t) 37 38 ns, client := SetupNamespace(t) 39 rpcAddr := randomPort() 40 41 // Disable caching to ensure we get a "build in progress" event each time. 42 skaffold.Dev("--cache-artifacts=false", "--rpc-port", rpcAddr).InDir("examples/getting-started").InNs(ns.Name).RunBackground(t) 43 client.WaitForPodsReady("getting-started") 44 45 // Ensure we see a build triggered in the event log 46 _, entries := apiEvents(t, rpcAddr) 47 48 failNowIfError(t, waitForEvent(90*time.Second, entries, func(e *proto.LogEntry) bool { 49 return e.GetEvent().GetBuildEvent().GetArtifact() == "skaffold-example" 50 })) 51 } 52 53 func TestCacheHits(t *testing.T) { 54 MarkIntegrationTest(t, CanRunWithoutGcp) 55 testutil.Run(t, "TestCacheHits", func(t *testutil.T) { 56 // Run skaffold build first to fail quickly on a build failure 57 skaffold.Build().InDir("examples/getting-started").RunOrFail(t.T) 58 59 ns, _ := SetupNamespace(t.T) 60 rpcAddr := randomPort() 61 62 // Rebuild with a different tag so that we get a cache hit. 63 out := skaffold.Build("--tag", ns.Name, "--rpc-port", rpcAddr).InDir("examples/getting-started").RunOrFailOutput(t.T) 64 t.CheckContains("skaffold-example: Found. Tagging", string(out)) 65 }) 66 } 67 68 func waitForEvent(timeout time.Duration, entries chan *proto.LogEntry, condition func(*proto.LogEntry) bool) error { 69 ctx, cancelTimeout := context.WithTimeout(context.Background(), timeout) 70 defer cancelTimeout() 71 for { 72 select { 73 case <-ctx.Done(): 74 return fmt.Errorf("timed out waiting for condition on log entry") 75 case ev := <-entries: 76 if condition(ev) { 77 return nil 78 } 79 } 80 } 81 } 82 83 func TestCacheIfBuildFail(t *testing.T) { 84 MarkIntegrationTest(t, CanRunWithoutGcp) 85 86 ns, _ := SetupNamespace(t) 87 88 cacheFile := "cache_" + ns.Name 89 testDir := "testdata/cache" 90 relativePath := path.Join(testDir, cacheFile) 91 defer os.Remove(relativePath) 92 93 skaffold.Build("--cache-file", cacheFile).InDir(testDir).InNs(ns.Name).Run(t) 94 95 fInfo, err := os.Stat(relativePath) 96 failNowIfError(t, err) 97 if b := fInfo.Size(); b == 0 { 98 failNowIfError(t, fmt.Errorf("expected to see content in the cache file, saw %d bytes", b)) 99 } 100 }