Skip to content

Commit

Permalink
fixes CI error and added more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Oct 31, 2024
1 parent cd7fb71 commit a858b57
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 38 deletions.
34 changes: 23 additions & 11 deletions Integrate-Checksum/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"os"
"runtime"
"testing"

Expand All @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions Integrate-Checksum/test_data/kcl.mod

This file was deleted.

5 changes: 0 additions & 5 deletions Integrate-Checksum/test_data/kcl.mod.lock

This file was deleted.

1 change: 0 additions & 1 deletion Integrate-Checksum/test_data/main.k

This file was deleted.

14 changes: 11 additions & 3 deletions mock/oci_env_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions mock/test_data/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[package]
name = "test_data"
edition = "v0.9.0"
version = "0.0.1"

[dependencies]
k8s = "1.31"

version = "0.0.1"
5 changes: 0 additions & 5 deletions mock/test_data/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.31"
version = "1.31"
3 changes: 3 additions & 0 deletions mock/test_script/cleanup_test_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/
31 changes: 31 additions & 0 deletions mock/test_script/pull_pkg.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a858b57

Please sign in to comment.