From eb2ae6c3984cae1e236b0a2b434a42627fd7dde5 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 13 Oct 2023 09:22:17 +0800 Subject: [PATCH] test: mock read release from cache --- common/fixtures/zimaos.go | 61 +++++++++++++++++++++++++ test/service/rauc_offline_test.go | 76 +++++++++++++++---------------- 2 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 common/fixtures/zimaos.go diff --git a/common/fixtures/zimaos.go b/common/fixtures/zimaos.go new file mode 100644 index 0000000..934cf9c --- /dev/null +++ b/common/fixtures/zimaos.go @@ -0,0 +1,61 @@ +package fixtures + +import ( + "path/filepath" + + "github.com/IceWhaleTech/CasaOS-Installer/codegen" + "github.com/IceWhaleTech/CasaOS-Installer/common" + "github.com/IceWhaleTech/CasaOS-Installer/internal" + "github.com/IceWhaleTech/CasaOS-Installer/internal/config" + "github.com/IceWhaleTech/CasaOS-Installer/service" +) + +const rauc_info_048 = `Compatible: 'zimaos-zimacube' +Version: '0.5.0.4' +Description: 'dmVyc2lvbjogdjAuNS4wLjQKcmVsZWFzZV9ub3RlczogfAogICMgcHJpdmF0ZSB0ZXN0Cm1pcnJvcnM6CiAgLSBodHRwczovL2Nhc2Fvcy5vc3MtY24tc2hhbmdoYWkuYWxpeXVuY3MuY29tL0ljZVdoYWxlVGVjaApwYWNrYWdlczoKICAtIHBhdGg6IC96aW1hb3MtcmF1Yy9yZWxlYXNlcy9kb3dubG9hZC90ZXN0L3ppbWFvc196aW1hY3ViZS0wLjQuOC5yYXVjYgogICAgYXJjaGl0ZWN0dXJlOiBhbWQ2NApjaGVja3N1bXM6IC9nZXQvcmVsZWFzZXMvZG93bmxvYWQvdjAuNC40LTEvY2hlY2tzdW1zLnR4dAptb2R1bGVzOgogIC0gbmFtZTogY2FzYW9zLWdhdGV3YXkKICAgIHNob3J0OiBnYXRld2F5CiAgLSBuYW1lOiBjYXNhb3MtdXNlci1zZXJ2aWNlCiAgICBzaG9ydDogdXNlci1zZXJ2aWNlCiAgLSBuYW1lOiBjYXNhb3MtbWVzc2FnZS1idXMKICAgIHNob3J0OiBtZXNzYWdlLWJ1cwogIC0gbmFtZTogY2FzYW9zCiAgICBzaG9ydDogY2FzYW9zCiAgLSBuYW1lOiBjYXNhb3MtbG9jYWwtc3RvcmFnZQogICAgc2hvcnQ6IGxvY2FsLXN0b3JhZ2UKICAtIG5hbWU6IGNhc2Fvcy1hcHAtbWFuYWdlbWVudAogICAgc2hvcnQ6IGFwcC1tYW5hZ2VtZW50' +Build: '(null)' +Hooks: 'install-check' +Bundle Format: plain + +3 Images: + [boot] + Filename: boot.vfat + Checksum: 055cbef657f1dcf1995124604994bc6d2d6477c47404f4bc11b6050666a4ffaa + Size: 33554432 + Hooks: install + [kernel] + Filename: kernel.img + Checksum: b39752abf9380e9e22c7e76bba898782056616c856823ab1a237cb9ae7e4b29b + Size: 14413824 + Hooks: post-install + [rootfs] + Filename: rootfs.img + Checksum: 390166cd2c16b0c8389ac814b01a5623b3137aea455c58f2727d5623cbd67b75 + Size: 490135552 + Hooks: + +Certificate Chain: + 0 Subject: O = IceWhale Technology, CN = IceWhale Technology Development-1 + Issuer: O = IceWhale Technology, CN = IceWhale Technology OTA Development + SPKI sha256: 96:A9:8A:2D:12:E3:6F:DE:ED:B1:0B:C8:26:2D:7C:EA:30:34:B5:15:1E:E6:AB:7C:DA:AD:F9:DC:DC:84:01:AD + Not Before: Jan 1 00:00:00 1970 GMT + Not After: Dec 31 23:59:59 9999 GMT + 1 Subject: O = IceWhale Technology, CN = IceWhale Technology OTA Development + Issuer: O = IceWhale Technology, CN = IceWhale Technology OTA Development + SPKI sha256: FE:BE:C2:D0:42:16:92:F4:85:5D:3D:71:C9:79:FC:D9:16:AE:9E:73:EB:74:48:4D:3E:D2:23:54:AF:D5:05:B4 + Not Before: Jan 1 00:00:00 1970 GMT + Not After: Dec 31 23:59:59 9999 GMT` + +func SetOfflineRAUCMock_0504(sysRoot string) error { + service.MockContent = rauc_info_048 + return nil +} + +func SetOfflineRAUCRelease_050(sysRoot string) error { + service.MockContent = rauc_info_048 + release := &codegen.Release{ + Version: "v0.5.0", + } + err := internal.WriteReleaseToLocal(release, filepath.Join(sysRoot, config.OFFLINE_RAUC_TEMP_PATH, common.ReleaseYAMLFileName)) + return err +} diff --git a/test/service/rauc_offline_test.go b/test/service/rauc_offline_test.go index beedbc9..f0bf24d 100644 --- a/test/service/rauc_offline_test.go +++ b/test/service/rauc_offline_test.go @@ -15,42 +15,6 @@ import ( "github.com/stretchr/testify/assert" ) -const rauc_info_048 = `Compatible: 'zimaos-zimacube' -Version: '0.4.8' -Description: 'dmVyc2lvbjogdjAuNS4wLjQKcmVsZWFzZV9ub3RlczogfAogICMgcHJpdmF0ZSB0ZXN0Cm1pcnJvcnM6CiAgLSBodHRwczovL2Nhc2Fvcy5vc3MtY24tc2hhbmdoYWkuYWxpeXVuY3MuY29tL0ljZVdoYWxlVGVjaApwYWNrYWdlczoKICAtIHBhdGg6IC96aW1hb3MtcmF1Yy9yZWxlYXNlcy9kb3dubG9hZC90ZXN0L3ppbWFvc196aW1hY3ViZS0wLjQuOC5yYXVjYgogICAgYXJjaGl0ZWN0dXJlOiBhbWQ2NApjaGVja3N1bXM6IC9nZXQvcmVsZWFzZXMvZG93bmxvYWQvdjAuNC40LTEvY2hlY2tzdW1zLnR4dAptb2R1bGVzOgogIC0gbmFtZTogY2FzYW9zLWdhdGV3YXkKICAgIHNob3J0OiBnYXRld2F5CiAgLSBuYW1lOiBjYXNhb3MtdXNlci1zZXJ2aWNlCiAgICBzaG9ydDogdXNlci1zZXJ2aWNlCiAgLSBuYW1lOiBjYXNhb3MtbWVzc2FnZS1idXMKICAgIHNob3J0OiBtZXNzYWdlLWJ1cwogIC0gbmFtZTogY2FzYW9zCiAgICBzaG9ydDogY2FzYW9zCiAgLSBuYW1lOiBjYXNhb3MtbG9jYWwtc3RvcmFnZQogICAgc2hvcnQ6IGxvY2FsLXN0b3JhZ2UKICAtIG5hbWU6IGNhc2Fvcy1hcHAtbWFuYWdlbWVudAogICAgc2hvcnQ6IGFwcC1tYW5hZ2VtZW50' -Build: '(null)' -Hooks: 'install-check' -Bundle Format: plain - -3 Images: - [boot] - Filename: boot.vfat - Checksum: 055cbef657f1dcf1995124604994bc6d2d6477c47404f4bc11b6050666a4ffaa - Size: 33554432 - Hooks: install - [kernel] - Filename: kernel.img - Checksum: b39752abf9380e9e22c7e76bba898782056616c856823ab1a237cb9ae7e4b29b - Size: 14413824 - Hooks: post-install - [rootfs] - Filename: rootfs.img - Checksum: 390166cd2c16b0c8389ac814b01a5623b3137aea455c58f2727d5623cbd67b75 - Size: 490135552 - Hooks: - -Certificate Chain: - 0 Subject: O = IceWhale Technology, CN = IceWhale Technology Development-1 - Issuer: O = IceWhale Technology, CN = IceWhale Technology OTA Development - SPKI sha256: 96:A9:8A:2D:12:E3:6F:DE:ED:B1:0B:C8:26:2D:7C:EA:30:34:B5:15:1E:E6:AB:7C:DA:AD:F9:DC:DC:84:01:AD - Not Before: Jan 1 00:00:00 1970 GMT - Not After: Dec 31 23:59:59 9999 GMT - 1 Subject: O = IceWhale Technology, CN = IceWhale Technology OTA Development - Issuer: O = IceWhale Technology, CN = IceWhale Technology OTA Development - SPKI sha256: FE:BE:C2:D0:42:16:92:F4:85:5D:3D:71:C9:79:FC:D9:16:AE:9E:73:EB:74:48:4D:3E:D2:23:54:AF:D5:05:B4 - Not Before: Jan 1 00:00:00 1970 GMT - Not After: Dec 31 23:59:59 9999 GMT` - func TestRAUCOfflineServer(t *testing.T) { logger.LogInitConsoleOnly() @@ -69,8 +33,6 @@ func TestRAUCOfflineServer(t *testing.T) { GetRAUCInfo: service.MockRAUCInfo, } - service.MockContent = rauc_info_048 - config.ServerInfo.CachePath = filepath.Join(tmpDir, "cache") config.SysRoot = tmpDir @@ -80,6 +42,7 @@ func TestRAUCOfflineServer(t *testing.T) { os.MkdirAll(filepath.Join(tmpDir, config.RAUC_OFFLINE_PATH), 0755) fixtures.SetOfflineRAUC(tmpDir, config.RAUC_OFFLINE_PATH, config.RAUC_OFFLINE_RAUC_FILENAME) + fixtures.SetOfflineRAUCMock_0504(tmpDir) release, err := installerServer.GetRelease(ctx, "any thing") assert.NoError(t, err) @@ -111,3 +74,40 @@ func TestRAUCOfflineServer(t *testing.T) { err = installerServer.Install(*release, tmpDir) assert.NoError(t, err) } + +func TestRAUCOfflineServerLoadReleaseFromCache(t *testing.T) { + logger.LogInitConsoleOnly() + + tmpDir, err := os.MkdirTemp("", "casaos-rauc-offline-extract-test-*") + assert.NoError(t, err) + // defer os.RemoveAll(tmpDir) + + ctx := context.Background() + assert.NoError(t, err) + config.ServerInfo.CachePath = filepath.Join(tmpDir, "cache") + + installerServer := &service.RAUCOfflineService{ + SysRoot: tmpDir, + InstallRAUCHandler: service.MockInstallRAUC, + CheckSumHandler: checksum.OfflineTarExistV2, + GetRAUCInfo: service.MockRAUCInfo, + } + + config.ServerInfo.CachePath = filepath.Join(tmpDir, "cache") + config.SysRoot = tmpDir + + // 构建假文件放到目录 + + config.RAUC_OFFLINE_RAUC_FILENAME = "rauc.raucb" + + os.MkdirAll(filepath.Join(tmpDir, config.RAUC_OFFLINE_PATH), 0755) + fixtures.SetOfflineRAUC(tmpDir, config.RAUC_OFFLINE_PATH, config.RAUC_OFFLINE_RAUC_FILENAME) + fixtures.SetOfflineRAUCMock_0504(tmpDir) + fixtures.SetOfflineRAUCRelease_050(tmpDir) + + release, err := installerServer.GetRelease(ctx, "any thing") + assert.NoError(t, err) + + assert.Equal(t, "v0.5.0", release.Version) + assert.Equal(t, "# private test\n", release.ReleaseNotes) +}