1 package storage 2 3 // TODO: swap this to f2 4 5 // import ( 6 // "context" 7 // "embed" 8 // "io" 9 // "os" 10 // "path/filepath" 11 // "testing" 12 13 // gcs "cloud.google.com/go/storage" 14 // "google.golang.org/api/iterator" 15 16 // "github.com/stretchr/testify/suite" 17 18 // "edge-infra.dev/test/framework" 19 // "edge-infra.dev/test/framework/gcp" 20 // "edge-infra.dev/test/framework/integration" 21 // ) 22 23 // //go:embed testdata/artifacts/* 24 // var artifacts embed.FS 25 26 // func TestMain(m *testing.M) { 27 // framework.HandleFlags() 28 // os.Exit(m.Run()) 29 // } 30 31 // type Suite struct { 32 // *framework.Framework 33 // ctx context.Context 34 // client *gcs.Client 35 // sclient *Storage 36 // storagePath string 37 // dir string 38 // names []string 39 // } 40 41 // func TestJobStorageClient(t *testing.T) { 42 // f := framework.New("devinfra-job-storage"). 43 // // Skip entire suite if this isn't an integration test run 44 // Setup(integration.SkipIfNot). 45 // // Skip entire suite if gcs bucket config isnt provided 46 // Setup(gcp.NeedsStorageBucket) 47 48 // s := &Suite{Framework: f, ctx: context.Background()} 49 // f.BeforeEachTest(s.beforeEach) 50 // f.AfterEachTest(s.afterEach) 51 52 // suite.Run(t, s) 53 // } 54 55 // func (s *Suite) TestUploadArtifacts() { 56 // err := s.sclient.UploadArtifacts(s.ctx, s.storagePath, s.dir) 57 // s.NoError(err) 58 59 // query := &gcs.Query{Prefix: filepath.Join(s.storagePath, "artifacts")} 60 // _ = query.SetAttrSelection([]string{"Name"}) 61 // it := s.sclient.BucketHandle.Objects(s.ctx, query) 62 // var storedNames []string 63 // count := 0 64 // for { 65 // attrs, err := it.Next() 66 // if err == iterator.Done { 67 // s.Equal(len(s.names), count) 68 // break 69 // } 70 // s.NoError(err) 71 // count++ 72 // storedNames = append(storedNames, filepath.Base(attrs.Name)) 73 // } 74 75 // s.Subset(s.names, storedNames) 76 // } 77 78 // func (s *Suite) TestRetrieveArtifacts() { 79 // s.NoError(s.sclient.UploadArtifacts(s.ctx, s.storagePath, s.dir)) 80 // artifacts, err := s.sclient.RetrieveArtifacts(s.ctx, s.storagePath) 81 // s.NoError(err) 82 // var data [][]byte 83 // for _, a := range artifacts { 84 // data = append(data, a.Contents) 85 // } 86 87 // query := &gcs.Query{Prefix: ArtifactsPath(s.storagePath)} 88 // _ = query.SetAttrSelection([]string{"Name"}) 89 // it := s.sclient.BucketHandle.Objects(s.ctx, query) 90 // var storedData [][]byte 91 // count := 0 92 // for { 93 // attrs, err := it.Next() 94 // if err == iterator.Done { 95 // s.Equal(len(s.names), count) 96 // break 97 // } 98 // s.NoError(err) 99 // obj := s.sclient.BucketHandle.Object(attrs.Name) 100 101 // r, err := obj.NewReader(s.ctx) 102 // s.NoError(err) 103 // defer r.Close() 104 105 // buf, err := io.ReadAll(r) 106 // s.NoError(err) 107 108 // s.NoError(err) 109 // count++ 110 // storedData = append(storedData, buf) 111 // } 112 113 // for _, d := range data { 114 // s.Contains(storedData, d) 115 // } 116 // } 117 118 // // re-assert internal suite state, providing clean run for each test 119 // func (s *Suite) beforeEach(f *framework.Framework) { 120 // s.Log(f.UniqueName) 121 // c, err := gcs.NewClient(s.ctx) 122 // if err != nil { 123 // s.FailNow("failed to create gcs client", err) 124 // } 125 // s.client = c 126 127 // s.setupTmpDir() 128 129 // s.storagePath = BasePath("edge-infra", f.BaseName, f.UniqueName, "ci") 130 131 // sclient, err := New(s.ctx, WithBucket(gcp.GCloud.StorageBucket), WithClient(s.client)) 132 // if err != nil { 133 // s.FailNow("failed to create job storage client", err) 134 // } 135 // s.sclient = sclient 136 // } 137 138 // func (s *Suite) afterEach(_ *framework.Framework) { 139 // s.NoError(os.RemoveAll(s.dir)) 140 // s.cleanupBucket() 141 // } 142 143 // func (s *Suite) setupTmpDir() { 144 // dir, err := os.CreateTemp("", "edge-infra-integration-test-") 145 // if err != nil { 146 // s.FailNow("failed to create tmpdir", err) 147 // } 148 // s.dir = dir.Name() 149 150 // var names []string 151 // // the storage library reads from the disk, so we need to write the 152 // // embedded test data files to some location on disk 153 // // - first read embedded files 154 // artifactFiles, err := artifacts.ReadDir("testdata/artifacts") 155 // if err != nil { 156 // s.FailNow("failed to read artifact dir", err) 157 // } 158 // // - then write them to disk 159 // for _, artifact := range artifactFiles { 160 // name := artifact.Name() 161 // names = append(names, name) 162 // data, err := artifacts.ReadFile(filepath.Join("testdata/artifacts/", name)) 163 // if err != nil { 164 // s.FailNow("failed to read embedded artifact file", err) 165 // } 166 167 // s.Log(os.Getenv("TEST_TMPDIR")) 168 // s.Log(filepath.Join(s.dir, name)) 169 170 // err = os.WriteFile(filepath.Join(s.dir, name), data, 0600) 171 // if err != nil { 172 // s.FailNow("failed to write embedded artifact file to tmpdir", err) 173 // } 174 // } 175 // s.names = names 176 // } 177 178 // func (s *Suite) cleanupBucket() { 179 // for _, name := range s.names { 180 // s.NoError( 181 // s.sclient.BucketHandle.Object(filepath.Join(s.storagePath, "artifacts", name)). 182 // Delete(s.ctx), 183 // ) 184 // } 185 // } 186