github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/automation/run/filestore_test.go (about)

     1  package run_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/qri-io/qri/automation/run"
    14  	"github.com/qri-io/qri/automation/spec"
    15  	"github.com/qri-io/qri/automation/workflow"
    16  	"github.com/qri-io/qri/event"
    17  )
    18  
    19  func TestFileStore(t *testing.T) {
    20  	ctx := context.Background()
    21  	tmpdir, err := ioutil.TempDir("", "")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer os.RemoveAll(tmpdir)
    26  
    27  	store, err := run.NewFileStore(tmpdir)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	spec.AssertRunStore(t, store)
    32  
    33  	counter := 0
    34  	NowFunc := func() *time.Time {
    35  		t := time.Unix(int64(1234000+counter), 0)
    36  		counter++
    37  		return &t
    38  	}
    39  	r1 := &run.State{
    40  		ID:         "run1",
    41  		WorkflowID: "workflow1",
    42  		Number:     0,
    43  		Status:     "success",
    44  		Message:    "message",
    45  		StartTime:  NowFunc(),
    46  		StopTime:   NowFunc(),
    47  		Duration:   1,
    48  		Steps: []*run.StepState{
    49  			{
    50  
    51  				Name:      "download",
    52  				Category:  "download",
    53  				Status:    "success",
    54  				StartTime: NowFunc(),
    55  				StopTime:  NowFunc(),
    56  				Duration:  1,
    57  				Output: []event.Event{
    58  					{
    59  						Type:      event.ETTransformStart,
    60  						Timestamp: 1,
    61  						SessionID: "sessionID",
    62  						Payload: event.TransformLifecycle{
    63  							StepCount: 1,
    64  							Status:    "success",
    65  						},
    66  					},
    67  					{
    68  						Type:      event.ETTransformStepStart,
    69  						Timestamp: 2,
    70  						Payload: event.TransformStepLifecycle{
    71  							Name:     "download",
    72  							Category: "download",
    73  							Status:   "error",
    74  						},
    75  					},
    76  					{
    77  						Type:      event.ETTransformPrint,
    78  						Timestamp: 3,
    79  						Payload: event.TransformMessage{
    80  							Msg: "printing!",
    81  						},
    82  					},
    83  				},
    84  			},
    85  		},
    86  	}
    87  	type WorkflowMeta struct {
    88  		Count  int      `json:"count"`
    89  		RunIDs []string `json:"runIDs"`
    90  	}
    91  	wfm := &WorkflowMeta{
    92  		Count:  1,
    93  		RunIDs: []string{"run1"},
    94  	}
    95  	mockStore := struct {
    96  		Workflows map[workflow.ID]*WorkflowMeta `json:"workflows"`
    97  		Runs      map[string]*run.State         `json:"runs"`
    98  	}{
    99  		Workflows: map[workflow.ID]*WorkflowMeta{
   100  			workflow.ID("workflow1"): wfm,
   101  		},
   102  		Runs: map[string]*run.State{
   103  			"run1": r1,
   104  		},
   105  	}
   106  	storeBytes, err := json.Marshal(mockStore)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  
   111  	if err := ioutil.WriteFile(filepath.Join(tmpdir, "runs.json"), storeBytes, 0644); err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	store, err = run.NewFileStore(tmpdir)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	gotRun, err := store.Get(ctx, "run1")
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	if diff := cmp.Diff(gotRun, r1); diff != "" {
   126  		t.Errorf("run mismatch (-want +got):\n%s", diff)
   127  	}
   128  
   129  	r2 := &run.State{
   130  		ID:         "run2",
   131  		WorkflowID: "workflow1",
   132  		Number:     0,
   133  		Status:     "success",
   134  		Message:    "message",
   135  		StartTime:  NowFunc(),
   136  		StopTime:   NowFunc(),
   137  		Duration:   1,
   138  		Steps: []*run.StepState{
   139  			{
   140  
   141  				Name:      "download",
   142  				Category:  "download",
   143  				Status:    "success",
   144  				StartTime: NowFunc(),
   145  				StopTime:  NowFunc(),
   146  				Duration:  1,
   147  				Output: []event.Event{
   148  					{
   149  						Type:      event.ETTransformStart,
   150  						Timestamp: 1,
   151  						SessionID: "sessionID",
   152  						Payload: event.TransformLifecycle{
   153  							StepCount: 1,
   154  							Status:    "success",
   155  						},
   156  					},
   157  					{
   158  						Type:      event.ETTransformStepStart,
   159  						Timestamp: 2,
   160  						Payload: event.TransformStepLifecycle{
   161  							Name:     "download",
   162  							Category: "download",
   163  							Status:   "error",
   164  						},
   165  					},
   166  					{
   167  						Type:      event.ETTransformPrint,
   168  						Timestamp: 3,
   169  						Payload: event.TransformMessage{
   170  							Msg: "printing!",
   171  						},
   172  					},
   173  				},
   174  			},
   175  		},
   176  	}
   177  
   178  	if _, err := store.Create(ctx, r2); err != nil {
   179  		t.Fatal(err)
   180  	}
   181  
   182  	if err := store.Shutdown(); err != nil {
   183  		t.Fatal(err)
   184  	}
   185  
   186  	wfm.Count = 2
   187  	wfm.RunIDs = []string{"run1", "run2"}
   188  	mockStore.Workflows["workflow1"] = wfm
   189  	mockStore.Runs["run2"] = r2
   190  
   191  	storeBytes, err = json.Marshal(mockStore)
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	gotBytes, err := ioutil.ReadFile(filepath.Join(tmpdir, "runs.json"))
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	if diff := cmp.Diff(string(storeBytes), string(gotBytes)); diff != "" {
   201  		t.Errorf("file mismatch (-want +got):\n%s", diff)
   202  	}
   203  }