github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/model/v1beta1/apiversion_convert_v1alpha1_test.go (about) 1 package v1beta1 2 3 import ( 4 "encoding/json" 5 "testing" 6 "time" 7 8 "github.com/filecoin-project/bacalhau/pkg/model/v1alpha1" 9 "github.com/go-test/deep" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func getJobs() (v1alpha1.Job, Job) { 14 nodeID := "test-node" 15 shardIndex := 0 16 concurrency := 3 17 jobID := "test-job" 18 clientID := "test-client" 19 CID := "QmX" 20 entrypoint := "hello" 21 stdout := "oranges" 22 status := "pineapples" 23 params := []string{"world"} 24 createdAt := time.Now() 25 26 v1alpha := v1alpha1.Job{ 27 APIVersion: V1alpha1.String(), 28 ID: jobID, 29 RequesterNodeID: nodeID, 30 ClientID: clientID, 31 CreatedAt: createdAt, 32 Spec: v1alpha1.Spec{ 33 Engine: v1alpha1.EngineWasm, 34 Wasm: v1alpha1.JobSpecWasm{ 35 EntryPoint: entrypoint, 36 Parameters: params, 37 }, 38 Inputs: []v1alpha1.StorageSpec{ 39 { 40 StorageSource: v1alpha1.StorageSourceIPFS, 41 CID: CID, 42 }, 43 }, 44 }, 45 Deal: v1alpha1.Deal{ 46 Concurrency: concurrency, 47 }, 48 ExecutionPlan: v1alpha1.JobExecutionPlan{ 49 TotalShards: concurrency, 50 }, 51 State: v1alpha1.JobState{ 52 Nodes: map[string]v1alpha1.JobNodeState{ 53 nodeID: { 54 Shards: map[int]v1alpha1.JobShardState{ 55 shardIndex: { 56 NodeID: nodeID, 57 ShardIndex: shardIndex, 58 State: v1alpha1.JobStateBidding, 59 Status: status, 60 PublishedResult: v1alpha1.StorageSpec{ 61 StorageSource: v1alpha1.StorageSourceIPFS, 62 CID: CID, 63 }, 64 RunOutput: &v1alpha1.RunCommandResult{ 65 STDOUT: stdout, 66 }, 67 }, 68 }, 69 }, 70 }, 71 }, 72 Events: []v1alpha1.JobEvent{ 73 { 74 APIVersion: V1alpha1.String(), 75 JobID: jobID, 76 ShardIndex: shardIndex, 77 ClientID: clientID, 78 SourceNodeID: nodeID, 79 TargetNodeID: nodeID, 80 EventName: v1alpha1.JobEventBid, 81 RunOutput: &v1alpha1.RunCommandResult{ 82 STDOUT: stdout, 83 }, 84 }, 85 }, 86 LocalEvents: []v1alpha1.JobLocalEvent{ 87 { 88 EventName: v1alpha1.JobLocalEventBid, 89 JobID: jobID, 90 ShardIndex: shardIndex, 91 TargetNodeID: nodeID, 92 }, 93 }, 94 } 95 96 latest := Job{ 97 APIVersion: APIVersionLatest().String(), 98 Metadata: Metadata{ 99 ID: jobID, 100 CreatedAt: createdAt, 101 ClientID: clientID, 102 }, 103 Spec: Spec{ 104 Engine: EngineWasm, 105 Wasm: JobSpecWasm{ 106 EntryPoint: entrypoint, 107 Parameters: params, 108 }, 109 Inputs: []StorageSpec{ 110 { 111 StorageSource: StorageSourceIPFS, 112 CID: CID, 113 }, 114 }, 115 Deal: Deal{ 116 Concurrency: concurrency, 117 }, 118 ExecutionPlan: JobExecutionPlan{ 119 TotalShards: concurrency, 120 }, 121 }, 122 Status: JobStatus{ 123 State: JobState{ 124 Nodes: map[string]JobNodeState{ 125 nodeID: { 126 Shards: map[int]JobShardState{ 127 shardIndex: { 128 NodeID: nodeID, 129 ShardIndex: shardIndex, 130 State: JobStateBidding, 131 Status: status, 132 PublishedResult: StorageSpec{ 133 StorageSource: StorageSourceIPFS, 134 CID: CID, 135 }, 136 RunOutput: &RunCommandResult{ 137 STDOUT: stdout, 138 }, 139 }, 140 }, 141 }, 142 }, 143 }, 144 Events: []JobEvent{ 145 { 146 APIVersion: APIVersionLatest().String(), 147 JobID: jobID, 148 ShardIndex: shardIndex, 149 ClientID: clientID, 150 SourceNodeID: nodeID, 151 TargetNodeID: nodeID, 152 EventName: JobEventBid, 153 RunOutput: &RunCommandResult{ 154 STDOUT: stdout, 155 }, 156 }, 157 }, 158 LocalEvents: []JobLocalEvent{ 159 { 160 EventName: JobLocalEventBid, 161 JobID: jobID, 162 ShardIndex: shardIndex, 163 TargetNodeID: nodeID, 164 }, 165 }, 166 Requester: JobRequester{ 167 RequesterNodeID: nodeID, 168 }, 169 }, 170 } 171 172 return v1alpha, latest 173 } 174 175 func TestParseAPIVersion(t *testing.T) { 176 v, err := ParseAPIVersion("V1beta1") 177 require.NoError(t, err) 178 require.Equal(t, v, V1beta1) 179 } 180 181 func TestConvertV1Alpha1_Job(t *testing.T) { 182 oldData, compareData := getJobs() 183 oldJSONString, err := json.Marshal(oldData) 184 require.NoError(t, err) 185 newData, err := APIVersionParseJob(V1alpha1.String(), string(oldJSONString)) 186 require.NoError(t, err) 187 if diff := deep.Equal(newData, compareData); diff != nil { 188 t.Error(diff) 189 } 190 }