diff --git a/cmd/archeio/main_test.go b/cmd/archeio/main_test.go index c9dd283..023e2f0 100644 --- a/cmd/archeio/main_test.go +++ b/cmd/archeio/main_test.go @@ -20,16 +20,26 @@ limitations under the License. package main import ( - "context" + "fmt" "net/http" "os" "os/exec" + "path/filepath" "testing" "time" + "github.com/google/go-containerregistry/cmd/crane/cmd" + "github.com/google/go-containerregistry/pkg/crane" "k8s.io/registry.k8s.io/internal/integration" ) +type integrationTestCase struct { + Name string + FakeIP string + Image string + Digest string +} + // TestIntegrationMain tests the entire, built binary with an integration // test, pulling images with crane func TestIntegrationMain(t *testing.T) { @@ -38,10 +48,6 @@ func TestIntegrationMain(t *testing.T) { if err != nil { t.Fatalf("Failed to detect module root dir: %v", err) } - // NOTE: also ensures rootDir/bin is in front of $PATH - if err := integration.EnsureCrane(rootDir); err != nil { - t.Fatalf("Failed to ensure crane: %v", err) - } // build binary buildCmd := exec.Command("make", "archeio") @@ -54,15 +60,22 @@ func TestIntegrationMain(t *testing.T) { testPort := "61337" testAddr := "localhost:" + testPort serverErrChan := make(chan error) - cmdContext, serverCancel := context.WithCancel(context.TODO()) - serverCmd := exec.CommandContext(cmdContext, "archeio") + serverCmd := exec.Command("./archeio", "-v=9") + serverCmd.Dir = filepath.Join(rootDir, "bin") serverCmd.Env = append(serverCmd.Env, "PORT="+testPort) - // serverCmd.Stderr = os.Stderr - defer serverCancel() + serverCmd.Stderr = os.Stderr go func() { serverErrChan <- serverCmd.Start() serverErrChan <- serverCmd.Wait() }() + t.Cleanup(func() { + if err := serverCmd.Process.Signal(os.Interrupt); err != nil { + t.Fatalf("failed to signal archeio: %v", err) + } + if err := <-serverErrChan; err != nil { + t.Fatalf("archeio did not exit cleanly: %v", err) + } + }) // wait for server to be up and running startErr := <-serverErrChan @@ -76,30 +89,105 @@ func TestIntegrationMain(t *testing.T) { t.Fatal("timed out waiting for archeio to be ready") } - // TODO: fake being on AWS - testPull := func(image string) { - // nolint:gosec // this is not user suplied input ... - cmd := exec.Command("crane", "pull", testAddr+"/"+image, os.DevNull) - //cmd.Stderr = os.Stderr - out, err := cmd.CombinedOutput() - if err != nil { - t.Errorf("pull for %q failed: %v", image, err) - t.Error("output: ") - t.Error(string(out)) - t.Fail() + // perform many test pulls ... + testCases := makeTestCases() + for i := range testCases { + tc := testCases[i] + t.Run(tc.Name, func(t *testing.T) { + t.Parallel() + ref := testAddr + "/" + tc.Image + // ensure we supply fake IP info from test case + craneOpts := []crane.Option{crane.WithTransport(newFakeIPTransport(tc.FakeIP))} + // test fetching digest first + digest, err := crane.Digest(ref, craneOpts...) + if err != nil { + t.Errorf("Fetch digest for %q failed: %v", ref, err) + } + if digest != tc.Digest { + t.Errorf("Wrong digest for %q", ref) + t.Errorf("Received: %q", digest) + t.Errorf("Expected: %q", tc.Digest) + } + err = pull(ref, craneOpts...) + if err != nil { + t.Errorf("Pull for %q failed: %v", ref, err) + } + }) + } +} + +func makeTestCases() []integrationTestCase { + wellKnownImages := []struct { + Name string + Digest string + }{ + { + Name: "pause:3.1", + Digest: "sha256:f78411e19d84a252e53bff71a4407a5686c46983a2c2eeed83929b888179acea", + }, + { + Name: "pause:3.9", + Digest: "sha256:7031c1b283388d2c2e09b57badb803c05ebed362dc88d84b480cc47f72a21097", + }, + } + + interestingIPs := []struct { + Name string + IP string + }{ + { + Name: "GCP", + IP: "35.220.26.1", + }, + { + Name: "AWS", + IP: "35.180.1.1", + }, + { + Name: "Definitely-External", + // we obviously won't see this in the wild, but we also know + // it should not match GCP, AWS or any future providers + IP: "192.168.0.1", + }, + } + + // generate testcases from test data + testCases := []integrationTestCase{} + for _, image := range wellKnownImages { + for _, ip := range interestingIPs { + testCases = append(testCases, integrationTestCase{ + Name: fmt.Sprintf("IP:%s (%q),Image:%q", ip.Name, ip.IP, image.Name), + FakeIP: ip.IP, + Image: image.Name, + Digest: image.Digest, + }) } } + return testCases +} - // test pulling pause image - // TODO: test pulling more things - testPull("pause:3.1") +func pull(image string, options ...crane.Option) error { + puller := cmd.NewCmdPull(&options) + puller.SetArgs([]string{image, "/dev/null"}) + return puller.Execute() +} - // we're done, cleanup - if err := serverCmd.Process.Signal(os.Interrupt); err != nil { - t.Fatalf("failed to signal archeio: %v", err) - } - if err := <-serverErrChan; err != nil { - t.Fatalf("archeio did not exit cleanly: %v", err) +type fakeIPTransport struct { + fakeXForwardFor string + h http.RoundTripper +} + +var _ http.RoundTripper = &fakeIPTransport{} + +func (f *fakeIPTransport) RoundTrip(r *http.Request) (*http.Response, error) { + r.Header.Add("X-Forwarded-For", f.fakeXForwardFor) + return f.h.RoundTrip(r) +} + +func newFakeIPTransport(fakeIP string) *fakeIPTransport { + return &fakeIPTransport{ + fakeXForwardFor: fakeIP + ",0.0.0.0", + h: http.DefaultTransport, } } diff --git a/go.mod b/go.mod index 6a3e4b3..d82b5c3 100644 --- a/go.mod +++ b/go.mod @@ -12,12 +12,15 @@ require ( require ( cloud.google.com/go/compute v1.10.0 // indirect + github.com/containerd/stargz-snapshotter/estargz v0.12.1 // indirect github.com/docker/cli v20.10.20+incompatible // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/docker/docker v20.10.20+incompatible // indirect github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/go-logr/logr v1.2.0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/compress v1.15.11 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect @@ -25,6 +28,9 @@ require ( github.com/opencontainers/image-spec v1.1.0-rc2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/sirupsen/logrus v1.9.0 // indirect + github.com/spf13/cobra v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/vbatts/tar-split v0.11.2 // indirect golang.org/x/net v0.8.0 // indirect golang.org/x/oauth2 v0.1.0 // indirect golang.org/x/sys v0.6.0 // indirect diff --git a/go.sum b/go.sum index 76dafb0..7373f7f 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ cloud.google.com/go/compute v1.10.0 h1:aoLIYaA1fX3ywihqpBk2APQKOo20nXsp1GEZQbx5Jk4= cloud.google.com/go/compute v1.10.0/go.mod h1:ER5CLbMxl90o2jtNbGSbtfOpQKR0t15FOtRsugnLrlU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/aws/aws-sdk-go v1.44.199 h1:hYuQmS4zLMJR9v2iOp2UOD6Vi/0V+nwyR/Uhrkrtlbc= github.com/aws/aws-sdk-go v1.44.199/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/containerd/stargz-snapshotter/estargz v0.12.1 h1:+7nYmHJb0tEkcRaAW+MHqoKaJYZmkikupxCqVtmPuY0= +github.com/containerd/stargz-snapshotter/estargz v0.12.1/go.mod h1:12VUuCq3qPq4y8yUW+l5w3+oXV3cx2Po3KSe/SmPGqw= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -22,8 +26,11 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-containerregistry v0.13.0 h1:y1C7Z3e149OJbOPDBxLYR8ITPz8dTKqQwjErKVHJC8k= github.com/google/go-containerregistry v0.13.0/go.mod h1:J9FQ+eSS4a1aC2GNZxvNpbWhgp0487v+cgiilB4FqDo= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -40,12 +47,23 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI= +github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vbatts/tar-split v0.11.2 h1:Via6XqJr0hceW4wff3QRzD5gAk/tatMw/4ZA7cTlIME= +github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaWq6kXyQ3VI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -60,10 +78,12 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/oauth2 v0.1.0 h1:isLCZuhj4v+tYv7eskaN4v/TM+A1begWWgyVJDdl1+Y= golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -95,10 +115,12 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=