Skip to content

Commit a858b57

Browse files
fixes CI error and added more tests
Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
1 parent cd7fb71 commit a858b57

File tree

9 files changed

+69
-38
lines changed

9 files changed

+69
-38
lines changed

Integrate-Checksum/main_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"encoding/json"
5-
"os"
65
"runtime"
76
"testing"
87

@@ -11,6 +10,7 @@ import (
1110
"gotest.tools/assert"
1211
"kcl-lang.io/kpm/pkg/client"
1312
"kcl-lang.io/kpm/pkg/constants"
13+
"kcl-lang.io/kpm/pkg/utils"
1414
)
1515

1616
// TestMainFunc tests the main functionality of the integrate checksum tool
@@ -24,20 +24,16 @@ func TestMainFunc(t *testing.T) {
2424
err := mock.StartDockerRegistry()
2525
assert.NilError(t, err)
2626

27-
// Push the test package to the local OCI registry
28-
err = mock.PushTestPkgToRegistry()
27+
// Push the test package to the local OCI registry.
28+
pkgDir, err := mock.PushTestPkgToRegistry()
2929
assert.NilError(t, err)
3030

3131
// Initialize the KPM client.
3232
kpmClient, err := client.NewKpmClient()
3333
assert.NilError(t, err, "Failed to initialize KPM client")
3434

35-
// Get the current working directory.
36-
currentDir, err := os.Getwd()
37-
assert.NilError(t, err, "Failed to get current working directory")
38-
3935
// Locate KCL module files in the current directory.
40-
packageDirs, err := findKCLModFiles(currentDir)
36+
packageDirs, err := findKCLModFiles(pkgDir)
4137
assert.NilError(t, err, "Failed to locate KCL module files")
4238
assert.Assert(t, len(packageDirs) > 0, "No KCL module files found")
4339

@@ -82,9 +78,7 @@ func TestMainFunc(t *testing.T) {
8278
assert.NilError(t, err, "Failed to marshal new manifest to JSON")
8379

8480
// Check if the manifest was updated correctly.
85-
if string(newManifestJSON) == string(originalManifestJSON) {
86-
t.Errorf("Failed to update the manifest; got %v", string(originalManifestJSON))
87-
}
81+
assert.Assert(t, string(newManifestJSON) != string(originalManifestJSON), "Failed to update the manifest")
8882

8983
// Revert the `Sum` field to its original value to ensure only that was changed.
9084
newManifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_SUM] = dependency.Sum
@@ -94,6 +88,24 @@ func TestMainFunc(t *testing.T) {
9488
// Compare the new manifest data with the expected manifest data.
9589
assert.Equal(t, string(newManifestJSON), string(originalManifestJSON), "New manifest data mismatch")
9690

91+
// Pull the test package.
92+
pkgPullPath, err := mock.PullTestPkg()
93+
assert.NilError(t, err)
94+
95+
// Find KCL module files in the pulled package.
96+
packagePullDir, err := findKCLModFiles(pkgPullPath)
97+
assert.NilError(t, err, "Failed to locate KCL module files")
98+
99+
// Ensure that at least one KCL module file was found.
100+
assert.Assert(t, len(packagePullDir) > 0, "No KCL module files found")
101+
102+
// Calculate the hash of the pulled KCL module directory to verify its integrity.
103+
pulledSum, err := utils.HashDir(packagePullDir[0])
104+
assert.NilError(t, err)
105+
106+
// Compare the hash of the pulled files with the expected dependency sum to check for unintended changes.
107+
assert.Equal(t, pulledSum, dependency.Sum, "Unexpected changes detected in the package contents")
108+
97109
// Clean the environment after all tests have been run
98110
err = mock.CleanTestEnv()
99111
assert.NilError(t, err)

Integrate-Checksum/test_data/kcl.mod

Lines changed: 0 additions & 8 deletions
This file was deleted.

Integrate-Checksum/test_data/kcl.mod.lock

Lines changed: 0 additions & 5 deletions
This file was deleted.

Integrate-Checksum/test_data/main.k

Lines changed: 0 additions & 1 deletion
This file was deleted.

mock/oci_env_mock.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ func StartDockerRegistry() error {
1010
return cmd.Run()
1111
}
1212

13-
// PushTestPkgToRegistry pushes the test package to the local Docker registry.
14-
func PushTestPkgToRegistry() error {
13+
// PushTestPkgToRegistry pushes the test package to the local Docker registry and returns directory location.
14+
func PushTestPkgToRegistry() (string, error) {
1515
cmd := exec.Command("../mock/test_script/push_pkg.sh")
16-
return cmd.Run()
16+
currentDir := "../mock"
17+
return currentDir, cmd.Run()
18+
}
19+
20+
// PullTestPkg pulls the test package from the local Docker registry.
21+
func PullTestPkg() (string, error) {
22+
cmd := exec.Command("../mock/test_script/pull_pkg.sh")
23+
pkgPullPath := "../mock/test_script"
24+
return pkgPullPath, cmd.Run()
1725
}
1826

1927
// CleanTestEnv cleans up the test environment by executing a cleanup script.

mock/test_data/kcl.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
[package]
22
name = "test_data"
33
edition = "v0.9.0"
4-
version = "0.0.1"
5-
6-
[dependencies]
7-
k8s = "1.31"
8-
4+
version = "0.0.1"

mock/test_data/kcl.mod.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
[dependencies]
2-
[dependencies.k8s]
3-
name = "k8s"
4-
full_name = "k8s_1.31"
5-
version = "1.31"

mock/test_script/cleanup_test_environment.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ rm -rf "$current_dir/scripts/"
1818
# Delete the 'kcl' binary
1919
cd "$SCRIPT_DIR/../../"
2020
rm -rf ./bin/
21+
22+
cd "$SCRIPT_DIR"
23+
rm -rf ./oci/

mock/test_script/pull_pkg.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Get the directory of the script
4+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
5+
6+
# Move to the root directory
7+
cd "$SCRIPT_DIR/../../"
8+
9+
# Install kcl binary
10+
GOBIN=$(pwd)/bin go install kcl-lang.io/cli/cmd/kcl@latest
11+
12+
# Check kpm version
13+
version=$(./bin/kcl --version)
14+
if ! echo "$version" ; then
15+
echo "Incorrect version: '$version'."
16+
exit 1
17+
fi
18+
19+
export KPM_REG="localhost:5001"
20+
export KPM_REPO="test"
21+
22+
# Prepare the package on the registry
23+
current_dir=$(pwd)
24+
echo $current_dir
25+
26+
# Log in to the local registry
27+
"$current_dir/bin/kcl" registry login -u test -p 1234 localhost:5001
28+
29+
# Pull the test_data package from the registry
30+
cd "$SCRIPT_DIR"
31+
"$current_dir/bin/kcl" mod pull oci://$KPM_REG/$KPM_REPO

0 commit comments

Comments
 (0)