| // Copyright 2025 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package version |
| |
| import ( |
| "context" |
| "fmt" |
| "os" |
| "path/filepath" |
| "reflect" |
| "testing" |
| |
| lab_platform "go.chromium.org/chromiumos/infra/proto/go/lab_platform" |
| |
| "go.chromium.org/infra/cros/recovery/models" |
| ) |
| |
| func makeTempDirWithStableVersionFile(board, model, content string) (string, error) { |
| dir, err := os.MkdirTemp("", "") |
| if err != nil { |
| return "", err |
| } |
| svFileName := fmt.Sprintf("%s-%s.json", board, model) |
| svFilePath := filepath.Join(dir, svFileName) |
| if err := os.WriteFile(svFilePath, []byte(content), 0644); err != nil { |
| return "", err |
| } |
| |
| return dir, nil |
| } |
| |
| func Test_toOSImagePath(t *testing.T) { |
| type args struct { |
| ctx context.Context |
| rv *models.RecoveryVersion |
| deviceType string |
| } |
| rvCrOS := &models.RecoveryVersion{ |
| Board: "brya", |
| OsImage: "R123-123.12.0", |
| } |
| rvAndroidOS := &models.RecoveryVersion{ |
| Board: "brya", |
| OsImage: "123456", |
| } |
| tests := []struct { |
| name string |
| args args |
| want string |
| }{ |
| {"empty type string should be CrOS image path", args{context.Background(), rvCrOS, ""}, "brya-release/R123-123.12.0"}, |
| {"cros type string should be CrOS image path", args{context.Background(), rvCrOS, "cros"}, "brya-release/R123-123.12.0"}, |
| {"androidos type string should be Android Desktop image path", args{context.Background(), rvAndroidOS, "androidos"}, "android-build/build_explorer/artifacts_list/123456/brya-trunk_staging-userdebug/brya-ota-123456.zip"}, |
| } |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| if got := toOSImagePath(tt.args.ctx, tt.args.rv, tt.args.deviceType); got != tt.want { |
| t.Errorf("toOSImagePath() = %v, want %v", got, tt.want) |
| } |
| }) |
| } |
| } |
| |
| func Test_readLocalVersionFile(t *testing.T) { |
| type args struct { |
| ctx context.Context |
| localDir string |
| board string |
| model string |
| deviceTypeStr string |
| } |
| |
| newFormatStableVersionContent := `{ |
| "versions": [ |
| { |
| "board": "volteer", |
| "model": "volta", |
| "os_image": "123456", |
| "fw_version": "Google_Volta.12345.123.0", |
| "fw_image": "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/", |
| "os_image_path": "android-build/build_explorer/artifacts_list/123456/volteer-trunk_staging-userdebug/volteer-ota-123456.zip", |
| "device_type": "androidos" |
| }, |
| { |
| "board": "volteer", |
| "model": "volta", |
| "os_image": "R123-12345.23.0", |
| "fw_version": "Google_Volta.12345.123.0", |
| "fw_image": "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/", |
| "os_image_path": "volteer-release/R123-12345.23.0", |
| "device_type": "cros" |
| } |
| ] |
| }` |
| svNewFormatDirPath, err := makeTempDirWithStableVersionFile("volteer", "volta", newFormatStableVersionContent) |
| if err != nil { |
| t.Errorf("Failed during setup temp file for the test: %s", err) |
| } |
| |
| oldFormatStableVersionContent := `{ |
| "board": "volteer", |
| "model": "volta", |
| "os_image": "R123-12345.23.0", |
| "fw_version": "Google_Volta.12345.123.0", |
| "fw_image": "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/" |
| }` |
| svOldFormatDirPath, err := makeTempDirWithStableVersionFile("volteer", "volta", oldFormatStableVersionContent) |
| if err != nil { |
| t.Errorf("Failed during setup temp file for the test: %s", err) |
| } |
| const ( |
| board = "volteer" |
| model = "volta" |
| crosType = "cros" |
| androidType = "androidos" |
| ) |
| |
| tests := []struct { |
| name string |
| args args |
| want Data |
| wantErr bool |
| }{ |
| { |
| name: "new format cros stable version", |
| args: args{context.Background(), svNewFormatDirPath, board, model, crosType}, |
| want: &lab_platform.StableVersion{ |
| Target: &lab_platform.StableVersionTarget{ |
| Board: board, |
| Model: model, |
| DeviceType: crosType, |
| }, |
| OsVersion: "R123-12345.23.0", |
| OsImagePath: "volteer-release/R123-12345.23.0", |
| FirmwareRoVersion: "Google_Volta.12345.123.0", |
| FirmwareRoImagePath: "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/", |
| }, |
| wantErr: false, |
| }, |
| { |
| name: "new format androidos stable version", |
| args: args{context.Background(), svNewFormatDirPath, board, model, androidType}, |
| want: &lab_platform.StableVersion{ |
| Target: &lab_platform.StableVersionTarget{ |
| Board: board, |
| Model: model, |
| DeviceType: androidType, |
| }, |
| OsVersion: "123456", |
| OsImagePath: "android-build/build_explorer/artifacts_list/123456/volteer-trunk_staging-userdebug/volteer-ota-123456.zip", |
| FirmwareRoVersion: "Google_Volta.12345.123.0", |
| FirmwareRoImagePath: "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/", |
| }, |
| wantErr: false, |
| }, |
| { |
| name: "old format cros stable version", |
| args: args{context.Background(), svOldFormatDirPath, board, model, crosType}, |
| want: &lab_platform.StableVersion{ |
| Target: &lab_platform.StableVersionTarget{ |
| Board: board, |
| Model: model, |
| DeviceType: crosType, |
| }, |
| OsVersion: "R123-12345.23.0", |
| OsImagePath: "volteer-release/R123-12345.23.0", |
| FirmwareRoVersion: "Google_Volta.12345.123.0", |
| FirmwareRoImagePath: "firmware-volteer-12345.B-branch-firmware/R100-12345.123.0/volteer/", |
| }, |
| wantErr: false, |
| }, |
| |
| { |
| name: "old format androidos stable version", |
| args: args{context.Background(), svOldFormatDirPath, board, model, androidType}, |
| want: nil, |
| wantErr: true, |
| }, |
| } |
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| got, err := readLocalVersionFile(tt.args.ctx, tt.args.localDir, tt.args.board, tt.args.model, tt.args.deviceTypeStr) |
| if (err != nil) != tt.wantErr { |
| t.Errorf("readLocalVersionFile() error = %v, wantErr %v", err, tt.wantErr) |
| return |
| } |
| if !reflect.DeepEqual(got, tt.want) { |
| t.Errorf("readLocalVersionFile() = %v, want %v", got, tt.want) |
| } |
| }) |
| } |
| } |