package storage // TODO: swap this to f2 // import ( // "context" // "embed" // "io" // "os" // "path/filepath" // "testing" // gcs "cloud.google.com/go/storage" // "google.golang.org/api/iterator" // "github.com/stretchr/testify/suite" // "edge-infra.dev/test/framework" // "edge-infra.dev/test/framework/gcp" // "edge-infra.dev/test/framework/integration" // ) // //go:embed testdata/artifacts/* // var artifacts embed.FS // func TestMain(m *testing.M) { // framework.HandleFlags() // os.Exit(m.Run()) // } // type Suite struct { // *framework.Framework // ctx context.Context // client *gcs.Client // sclient *Storage // storagePath string // dir string // names []string // } // func TestJobStorageClient(t *testing.T) { // f := framework.New("devinfra-job-storage"). // // Skip entire suite if this isn't an integration test run // Setup(integration.SkipIfNot). // // Skip entire suite if gcs bucket config isnt provided // Setup(gcp.NeedsStorageBucket) // s := &Suite{Framework: f, ctx: context.Background()} // f.BeforeEachTest(s.beforeEach) // f.AfterEachTest(s.afterEach) // suite.Run(t, s) // } // func (s *Suite) TestUploadArtifacts() { // err := s.sclient.UploadArtifacts(s.ctx, s.storagePath, s.dir) // s.NoError(err) // query := &gcs.Query{Prefix: filepath.Join(s.storagePath, "artifacts")} // _ = query.SetAttrSelection([]string{"Name"}) // it := s.sclient.BucketHandle.Objects(s.ctx, query) // var storedNames []string // count := 0 // for { // attrs, err := it.Next() // if err == iterator.Done { // s.Equal(len(s.names), count) // break // } // s.NoError(err) // count++ // storedNames = append(storedNames, filepath.Base(attrs.Name)) // } // s.Subset(s.names, storedNames) // } // func (s *Suite) TestRetrieveArtifacts() { // s.NoError(s.sclient.UploadArtifacts(s.ctx, s.storagePath, s.dir)) // artifacts, err := s.sclient.RetrieveArtifacts(s.ctx, s.storagePath) // s.NoError(err) // var data [][]byte // for _, a := range artifacts { // data = append(data, a.Contents) // } // query := &gcs.Query{Prefix: ArtifactsPath(s.storagePath)} // _ = query.SetAttrSelection([]string{"Name"}) // it := s.sclient.BucketHandle.Objects(s.ctx, query) // var storedData [][]byte // count := 0 // for { // attrs, err := it.Next() // if err == iterator.Done { // s.Equal(len(s.names), count) // break // } // s.NoError(err) // obj := s.sclient.BucketHandle.Object(attrs.Name) // r, err := obj.NewReader(s.ctx) // s.NoError(err) // defer r.Close() // buf, err := io.ReadAll(r) // s.NoError(err) // s.NoError(err) // count++ // storedData = append(storedData, buf) // } // for _, d := range data { // s.Contains(storedData, d) // } // } // // re-assert internal suite state, providing clean run for each test // func (s *Suite) beforeEach(f *framework.Framework) { // s.Log(f.UniqueName) // c, err := gcs.NewClient(s.ctx) // if err != nil { // s.FailNow("failed to create gcs client", err) // } // s.client = c // s.setupTmpDir() // s.storagePath = BasePath("edge-infra", f.BaseName, f.UniqueName, "ci") // sclient, err := New(s.ctx, WithBucket(gcp.GCloud.StorageBucket), WithClient(s.client)) // if err != nil { // s.FailNow("failed to create job storage client", err) // } // s.sclient = sclient // } // func (s *Suite) afterEach(_ *framework.Framework) { // s.NoError(os.RemoveAll(s.dir)) // s.cleanupBucket() // } // func (s *Suite) setupTmpDir() { // dir, err := os.CreateTemp("", "edge-infra-integration-test-") // if err != nil { // s.FailNow("failed to create tmpdir", err) // } // s.dir = dir.Name() // var names []string // // the storage library reads from the disk, so we need to write the // // embedded test data files to some location on disk // // - first read embedded files // artifactFiles, err := artifacts.ReadDir("testdata/artifacts") // if err != nil { // s.FailNow("failed to read artifact dir", err) // } // // - then write them to disk // for _, artifact := range artifactFiles { // name := artifact.Name() // names = append(names, name) // data, err := artifacts.ReadFile(filepath.Join("testdata/artifacts/", name)) // if err != nil { // s.FailNow("failed to read embedded artifact file", err) // } // s.Log(os.Getenv("TEST_TMPDIR")) // s.Log(filepath.Join(s.dir, name)) // err = os.WriteFile(filepath.Join(s.dir, name), data, 0600) // if err != nil { // s.FailNow("failed to write embedded artifact file to tmpdir", err) // } // } // s.names = names // } // func (s *Suite) cleanupBucket() { // for _, name := range s.names { // s.NoError( // s.sclient.BucketHandle.Object(filepath.Join(s.storagePath, "artifacts", name)). // Delete(s.ctx), // ) // } // }