diff --git a/Integrate-Checksum/main_test.go b/Integrate-Checksum/main_test.go index 589322fd..557c7912 100644 --- a/Integrate-Checksum/main_test.go +++ b/Integrate-Checksum/main_test.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "os" "runtime" "testing" @@ -11,6 +10,7 @@ import ( "gotest.tools/assert" "kcl-lang.io/kpm/pkg/client" "kcl-lang.io/kpm/pkg/constants" + "kcl-lang.io/kpm/pkg/utils" ) // TestMainFunc tests the main functionality of the integrate checksum tool @@ -24,20 +24,16 @@ func TestMainFunc(t *testing.T) { err := mock.StartDockerRegistry() assert.NilError(t, err) - // Push the test package to the local OCI registry - err = mock.PushTestPkgToRegistry() + // Push the test package to the local OCI registry. + pkgDir, err := mock.PushTestPkgToRegistry() assert.NilError(t, err) // Initialize the KPM client. kpmClient, err := client.NewKpmClient() assert.NilError(t, err, "Failed to initialize KPM client") - // Get the current working directory. - currentDir, err := os.Getwd() - assert.NilError(t, err, "Failed to get current working directory") - // Locate KCL module files in the current directory. - packageDirs, err := findKCLModFiles(currentDir) + packageDirs, err := findKCLModFiles(pkgDir) assert.NilError(t, err, "Failed to locate KCL module files") assert.Assert(t, len(packageDirs) > 0, "No KCL module files found") @@ -82,9 +78,7 @@ func TestMainFunc(t *testing.T) { assert.NilError(t, err, "Failed to marshal new manifest to JSON") // Check if the manifest was updated correctly. - if string(newManifestJSON) == string(originalManifestJSON) { - t.Errorf("Failed to update the manifest; got %v", string(originalManifestJSON)) - } + assert.Assert(t, string(newManifestJSON) != string(originalManifestJSON), "Failed to update the manifest") // Revert the `Sum` field to its original value to ensure only that was changed. newManifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_SUM] = dependency.Sum @@ -94,6 +88,24 @@ func TestMainFunc(t *testing.T) { // Compare the new manifest data with the expected manifest data. assert.Equal(t, string(newManifestJSON), string(originalManifestJSON), "New manifest data mismatch") + // Pull the test package. + pkgPullPath, err := mock.PullTestPkg() + assert.NilError(t, err) + + // Find KCL module files in the pulled package. + packagePullDir, err := findKCLModFiles(pkgPullPath) + assert.NilError(t, err, "Failed to locate KCL module files") + + // Ensure that at least one KCL module file was found. + assert.Assert(t, len(packagePullDir) > 0, "No KCL module files found") + + // Calculate the hash of the pulled KCL module directory to verify its integrity. + pulledSum, err := utils.HashDir(packagePullDir[0]) + assert.NilError(t, err) + + // Compare the hash of the pulled files with the expected dependency sum to check for unintended changes. + assert.Equal(t, pulledSum, dependency.Sum, "Unexpected changes detected in the package contents") + // Clean the environment after all tests have been run err = mock.CleanTestEnv() assert.NilError(t, err) diff --git a/Integrate-Checksum/test_data/kcl.mod b/Integrate-Checksum/test_data/kcl.mod deleted file mode 100644 index 6c01364d..00000000 --- a/Integrate-Checksum/test_data/kcl.mod +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "test_data" -edition = "v0.9.0" -version = "0.0.1" - -[dependencies] -k8s = "1.31" - diff --git a/Integrate-Checksum/test_data/kcl.mod.lock b/Integrate-Checksum/test_data/kcl.mod.lock deleted file mode 100644 index 41b4867f..00000000 --- a/Integrate-Checksum/test_data/kcl.mod.lock +++ /dev/null @@ -1,5 +0,0 @@ -[dependencies] - [dependencies.k8s] - name = "k8s" - full_name = "k8s_1.31" - version = "1.31" diff --git a/Integrate-Checksum/test_data/main.k b/Integrate-Checksum/test_data/main.k deleted file mode 100644 index fa7048e6..00000000 --- a/Integrate-Checksum/test_data/main.k +++ /dev/null @@ -1 +0,0 @@ -The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/mock/oci_env_mock.go b/mock/oci_env_mock.go index 4ebd5819..f9b2a32b 100644 --- a/mock/oci_env_mock.go +++ b/mock/oci_env_mock.go @@ -10,10 +10,18 @@ func StartDockerRegistry() error { return cmd.Run() } -// PushTestPkgToRegistry pushes the test package to the local Docker registry. -func PushTestPkgToRegistry() error { +// PushTestPkgToRegistry pushes the test package to the local Docker registry and returns directory location. +func PushTestPkgToRegistry() (string, error) { cmd := exec.Command("../mock/test_script/push_pkg.sh") - return cmd.Run() + currentDir := "../mock" + return currentDir, cmd.Run() +} + +// PullTestPkg pulls the test package from the local Docker registry. +func PullTestPkg() (string, error) { + cmd := exec.Command("../mock/test_script/pull_pkg.sh") + pkgPullPath := "../mock/test_script" + return pkgPullPath, cmd.Run() } // CleanTestEnv cleans up the test environment by executing a cleanup script. diff --git a/mock/test_data/kcl.mod b/mock/test_data/kcl.mod index 6c01364d..d0044fa4 100644 --- a/mock/test_data/kcl.mod +++ b/mock/test_data/kcl.mod @@ -1,8 +1,4 @@ [package] name = "test_data" edition = "v0.9.0" -version = "0.0.1" - -[dependencies] -k8s = "1.31" - +version = "0.0.1" \ No newline at end of file diff --git a/mock/test_data/kcl.mod.lock b/mock/test_data/kcl.mod.lock index 41b4867f..e69de29b 100644 --- a/mock/test_data/kcl.mod.lock +++ b/mock/test_data/kcl.mod.lock @@ -1,5 +0,0 @@ -[dependencies] - [dependencies.k8s] - name = "k8s" - full_name = "k8s_1.31" - version = "1.31" diff --git a/mock/test_script/cleanup_test_environment.sh b/mock/test_script/cleanup_test_environment.sh index 4a244a01..2857c8a7 100755 --- a/mock/test_script/cleanup_test_environment.sh +++ b/mock/test_script/cleanup_test_environment.sh @@ -18,3 +18,6 @@ rm -rf "$current_dir/scripts/" # Delete the 'kcl' binary cd "$SCRIPT_DIR/../../" rm -rf ./bin/ + +cd "$SCRIPT_DIR" +rm -rf ./oci/ \ No newline at end of file diff --git a/mock/test_script/pull_pkg.sh b/mock/test_script/pull_pkg.sh new file mode 100755 index 00000000..9304665d --- /dev/null +++ b/mock/test_script/pull_pkg.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Get the directory of the script +SCRIPT_DIR="$(dirname "$(realpath "$0")")" + +# Move to the root directory +cd "$SCRIPT_DIR/../../" + +# Install kcl binary +GOBIN=$(pwd)/bin go install kcl-lang.io/cli/cmd/kcl@latest + +# Check kpm version +version=$(./bin/kcl --version) +if ! echo "$version" ; then + echo "Incorrect version: '$version'." + exit 1 +fi + +export KPM_REG="localhost:5001" +export KPM_REPO="test" + +# Prepare the package on the registry +current_dir=$(pwd) +echo $current_dir + +# Log in to the local registry +"$current_dir/bin/kcl" registry login -u test -p 1234 localhost:5001 + +# Pull the test_data package from the registry +cd "$SCRIPT_DIR" +"$current_dir/bin/kcl" mod pull oci://$KPM_REG/$KPM_REPO