github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/automation/workflow/filestore_test.go (about) 1 package workflow_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "io/ioutil" 7 "log" 8 "os" 9 "path/filepath" 10 "testing" 11 "time" 12 13 "github.com/google/go-cmp/cmp" 14 "github.com/qri-io/qri/automation/spec" 15 "github.com/qri-io/qri/automation/workflow" 16 "github.com/qri-io/qri/profile" 17 ) 18 19 func TestFileStoreIntegration(t *testing.T) { 20 ctx := context.Background() 21 tmpdir, err := ioutil.TempDir("", "") 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer os.RemoveAll(tmpdir) 26 27 store, err := workflow.NewFileStore(tmpdir) 28 if err != nil { 29 t.Fatalf("NewFileStore unexpected error: %s", err) 30 } 31 spec.AssertWorkflowStore(t, store) 32 33 err = os.Remove(filepath.Join(tmpdir, "workflows.json")) 34 if err != nil { 35 t.Fatalf("removing workflow.json file error: %s", err) 36 } 37 38 store, err = workflow.NewFileStore(tmpdir) 39 if err != nil { 40 t.Fatalf("NewFileStore unexpected error: %s", err) 41 } 42 spec.AssertWorkflowLister(t, store) 43 44 timestamp := time.Unix(0, 123400000) 45 expectedWF1 := &workflow.Workflow{ 46 ID: "workflow1", 47 InitID: "dataset1", 48 OwnerID: profile.IDB58MustDecode("QmTwtwLMKHHKCrugNxyAaZ31nhBqRUQVysT2xK911n4m6F"), 49 Created: ×tamp, 50 Active: true, 51 Triggers: []map[string]interface{}{ 52 {"id": "trigger1"}, 53 }, 54 Hooks: []map[string]interface{}{ 55 {"id": "hook1"}, 56 }, 57 } 58 set := struct { 59 Workflows []*workflow.Workflow `json:"workflows"` 60 }{ 61 Workflows: []*workflow.Workflow{expectedWF1}, 62 } 63 64 // write workflow store json 65 wfsBytes, err := json.Marshal(set) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if err := ioutil.WriteFile(filepath.Join(tmpdir, "workflows.json"), wfsBytes, 0644); err != nil { 71 t.Fatal(err) 72 } 73 74 store, err = workflow.NewFileStore(tmpdir) 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 got, err := store.Get(ctx, expectedWF1.ID) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 if diff := cmp.Diff(expectedWF1, got); diff != "" { 85 t.Errorf("workflow mismatch (-want +got):\n%s", diff) 86 } 87 88 // add to it 89 expectedWF2 := &workflow.Workflow{ 90 ID: "workflow2", 91 InitID: "dataset2", 92 OwnerID: profile.IDB58MustDecode("QmTwtwLMKHHKCrugNxyAaZ31nhBqRUQVysT2xK911n4m6F"), 93 Created: ×tamp, 94 Active: false, 95 Triggers: []map[string]interface{}{ 96 {"id": "trigger2"}, 97 }, 98 Hooks: []map[string]interface{}{ 99 {"id": "hook2"}, 100 }, 101 } 102 set.Workflows = []*workflow.Workflow{expectedWF1, expectedWF2} 103 wfsBytes, err = json.Marshal(set) 104 if err != nil { 105 t.Fatal(err) 106 } 107 if _, err = store.Put(ctx, expectedWF2); err != nil { 108 t.Fatal(err) 109 } 110 if err := store.Shutdown(ctx); err != nil { 111 t.Fatal(err) 112 } 113 gotBytes, err := ioutil.ReadFile(filepath.Join(tmpdir, "workflows.json")) 114 if err != nil { 115 t.Fatal(err) 116 } 117 if diff := cmp.Diff(string(wfsBytes), string(gotBytes)); diff != "" { 118 t.Errorf("file mismatch (-want +got):\n%s", diff) 119 } 120 }