github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/bigquerytranslator/translator_test.go (about)

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bigquerytranslator
    16  
    17  import (
    18  	"testing"
    19  
    20  	"cloud.google.com/go/bigquery"
    21  	"github.com/google/go-cmp/cmp"
    22  
    23  	lpb "github.com/bazelbuild/reclient/api/log"
    24  
    25  	cpb "github.com/bazelbuild/remote-apis-sdks/go/api/command"
    26  )
    27  
    28  func TestItemSave(t *testing.T) {
    29  	i := &Item{
    30  		LogRecord: &lpb.LogRecord{
    31  			Command: &cpb.Command{
    32  				Identifiers: &cpb.Identifiers{
    33  					CommandId:    "a",
    34  					InvocationId: "b",
    35  					ToolName:     "c",
    36  					ExecutionId:  "d",
    37  				},
    38  				Args:     []string{"a", "b", "c"},
    39  				ExecRoot: "/exec/root",
    40  				Input: &cpb.InputSpec{
    41  					Inputs: []string{"foo.h", "bar.h"},
    42  					ExcludeInputs: []*cpb.ExcludeInput{
    43  						&cpb.ExcludeInput{
    44  							Regex: "*.bla",
    45  							Type:  cpb.InputType_DIRECTORY,
    46  						},
    47  						&cpb.ExcludeInput{
    48  							Regex: "*.blo",
    49  							Type:  cpb.InputType_FILE,
    50  						},
    51  					},
    52  					EnvironmentVariables: map[string]string{
    53  						"k":  "v",
    54  						"k1": "v1",
    55  					},
    56  				},
    57  				Output: &cpb.OutputSpec{
    58  					OutputFiles: []string{"a/b/out"},
    59  				},
    60  			},
    61  			Result: &cpb.CommandResult{
    62  				Status:   cpb.CommandResultStatus_CACHE_HIT,
    63  				ExitCode: 42,
    64  				Msg:      "message",
    65  			},
    66  			RemoteMetadata: &lpb.RemoteMetadata{
    67  				Result: &cpb.CommandResult{
    68  					Status:   cpb.CommandResultStatus_CACHE_HIT,
    69  					ExitCode: 42,
    70  					Msg:      "message",
    71  				},
    72  				CacheHit:            true,
    73  				NumInputFiles:       2,
    74  				NumInputDirectories: 3,
    75  				TotalInputBytes:     4,
    76  				CommandDigest:       "abc/10",
    77  				ActionDigest:        "def/2",
    78  			},
    79  			LocalMetadata: &lpb.LocalMetadata{
    80  				ValidCacheHit:   true,
    81  				ExecutedLocally: false,
    82  				UpdatedCache:    true,
    83  			},
    84  			CompletionStatus: lpb.CompletionStatus_STATUS_CACHE_HIT,
    85  		},
    86  	}
    87  
    88  	want := map[string]bigquery.Value{
    89  		"command": map[string]bigquery.Value{
    90  			"identifiers": map[string]bigquery.Value{
    91  				"command_id":                "a",
    92  				"invocation_id":             "b",
    93  				"correlated_invocations_id": "",
    94  				"tool_name":                 "c",
    95  				"tool_version":              "",
    96  				"execution_id":              "d",
    97  			},
    98  			"exec_root": "/exec/root",
    99  			"input": map[string]bigquery.Value{
   100  				"inputs":         []string{"foo.h", "bar.h"},
   101  				"virtual_inputs": []map[string]bigquery.Value{},
   102  				"exclude_inputs": []map[string]bigquery.Value{
   103  					{
   104  						"regex": "*.bla",
   105  						"type":  cpb.InputType_DIRECTORY,
   106  					},
   107  					{
   108  						"regex": "*.blo",
   109  						"type":  cpb.InputType_FILE,
   110  					},
   111  				},
   112  				"environment_variables": []map[string]bigquery.Value{
   113  					{
   114  						"key":   "k",
   115  						"value": "v",
   116  					},
   117  					{
   118  						"key":   "k1",
   119  						"value": "v1",
   120  					},
   121  				},
   122  			},
   123  			"output": map[string]bigquery.Value{
   124  				"output_files": []string{"a/b/out"},
   125  			},
   126  			"args":              []string{"a", "b", "c"},
   127  			"execution_timeout": int32(0),
   128  			"working_directory": "",
   129  			"platform":          []map[string]bigquery.Value(nil),
   130  		},
   131  		"local_metadata": map[string]bigquery.Value{
   132  			"result": map[string]bigquery.Value{
   133  				"exit_code": int32(0),
   134  				"msg":       "",
   135  				"status":    cpb.CommandResultStatus_UNKNOWN,
   136  			},
   137  			"executed_locally": false,
   138  			"valid_cache_hit":  true,
   139  			"updated_cache":    true,
   140  			"environment":      []map[string]bigquery.Value{},
   141  			"event_times":      []map[string]bigquery.Value{},
   142  			"labels":           []map[string]bigquery.Value{},
   143  			"verification": map[string]bigquery.Value{
   144  				"mismatches":       []map[string]bigquery.Value{},
   145  				"total_mismatches": int32(0),
   146  			},
   147  		},
   148  		"remote_metadata": map[string]bigquery.Value{
   149  			"action_digest":            "def/2",
   150  			"cache_hit":                true,
   151  			"command_digest":           "abc/10",
   152  			"event_times":              []map[string]bigquery.Value{},
   153  			"num_input_directories":    int32(3),
   154  			"num_input_files":          int32(2),
   155  			"num_output_directories":   int32(0),
   156  			"num_output_files":         int32(0),
   157  			"logical_bytes_downloaded": int64(0),
   158  			"logical_bytes_uploaded":   int64(0),
   159  			"real_bytes_downloaded":    int64(0),
   160  			"real_bytes_uploaded":      int64(0),
   161  			"stderr_digest":            "",
   162  			"stdout_digest":            "",
   163  			"output_directory_digests": []map[string]bigquery.Value{},
   164  			"output_file_digests":      []map[string]bigquery.Value{},
   165  			"result": map[string]bigquery.Value{
   166  				"exit_code": int32(42),
   167  				"msg":       "message",
   168  				"status":    cpb.CommandResultStatus_CACHE_HIT,
   169  			},
   170  			"total_input_bytes":  int64(4),
   171  			"total_output_bytes": int64(0),
   172  		},
   173  		"result": map[string]bigquery.Value{
   174  			"exit_code": int32(42),
   175  			"msg":       "message",
   176  			"status":    cpb.CommandResultStatus_CACHE_HIT,
   177  		},
   178  		"completion_status": lpb.CompletionStatus_STATUS_CACHE_HIT,
   179  	}
   180  	wantID := "a"
   181  
   182  	got, gotID, err := i.Save()
   183  	if err != nil {
   184  		t.Errorf("Item.Save() failed: %v", err)
   185  	}
   186  
   187  	if diff := cmp.Diff(want, got); diff != "" {
   188  		t.Errorf("Item.Save() returned diff, (-want +got): %v", diff)
   189  	}
   190  	if gotID != wantID {
   191  		t.Errorf("Item.Save() returned different IDs, want: %v, got: %v", wantID, gotID)
   192  	}
   193  }