Skip to content

Commit

Permalink
Added github action to run integrate checksum tool
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Oct 13, 2024
1 parent 4c44ded commit cd7fb71
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/include-kcl-checksums.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Include KCL Modules Checksum

on:
push:

jobs:
include_modules_checksum:
runs-on: ubuntu-latest

steps:
- name: Install kcl
run: wget -q https://kcl-lang.io/script/install-cli.sh -O - | /bin/bash

- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Login to GHCR
run: kcl registry login -u ${{ secrets.DEPLOY_ACCESS_NAME }} -p ${{ secrets.DEPLOY_ACCESS_TOKEN }} ghcr.io

- name: Get dependencies
run: go get -v ./...

- name: Run include checksum tool
run: go run ./Integrate-Checksum/main.go
2 changes: 2 additions & 0 deletions Integrate-Checksum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ func main() {
fmt.Printf("Error processing package at '%s': %v\n", packageDir, err)
}
}

fmt.Println("Checksum successfully included in all KCL packages")
}
100 changes: 100 additions & 0 deletions Integrate-Checksum/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

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

"artifacthub/mock"

"gotest.tools/assert"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/constants"
)

// TestMainFunc tests the main functionality of the integrate checksum tool
func TestMainFunc(t *testing.T) {
// Skip the test on Windows due to platform-specific issues.
if runtime.GOOS == "windows" {
t.Skip("Skipping TestMainFunc on Windows due to platform-specific issues")
}

// Start the local Docker registry required for testing
err := mock.StartDockerRegistry()
assert.NilError(t, err)

// Push the test package to the local OCI registry
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)
assert.NilError(t, err, "Failed to locate KCL module files")
assert.Assert(t, len(packageDirs) > 0, "No KCL module files found")

// Resolve the dependency for the first module found.
dependency, err := resolveDependency(kpmClient, packageDirs[0])
assert.NilError(t, err, "Failed to resolve dependency")

// Set custom OCI registry and repository for testing.
dependency.Source.Oci.Reg = "localhost:5001"
dependency.Source.Oci.Repo = "test"

// Fetch the original manifest.
originalManifest, err := fetchManifest(kpmClient, dependency)
assert.NilError(t, err, "Failed to fetch original manifest")

// Marshal the original manifest into JSON format.
originalManifestJSON, err := json.Marshal(originalManifest)
assert.NilError(t, err, "Failed to marshal original manifest to JSON")

// Configure the repository for testing purposes.
repository, err := configureRepository(dependency, kpmClient)
assert.NilError(t, err, "Failed to configure repository")
repository.PlainHTTP = true // Enable plain HTTP for local testing.

// Modify the manifest annotations for testing.
originalManifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_SUM] = "changes-for-testing-purpose"

// Marshal the updated manifest into JSON format.
updatedManifestJSON, err := json.Marshal(originalManifest)
assert.NilError(t, err, "Failed to marshal updated manifest to JSON")

// Tag the updated manifest in the repository.
err = tagManifest(repository, updatedManifestJSON, dependency)
assert.NilError(t, err, "Failed to tag updated manifest in repository")

// Fetch the new manifest after tagging.
newManifest, err := fetchManifest(kpmClient, dependency)
assert.NilError(t, err, "Failed to fetch new manifest")

// Marshal the new manifest into JSON format for comparison.
newManifestJSON, err := json.Marshal(newManifest)
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))
}

// Revert the `Sum` field to its original value to ensure only that was changed.
newManifest.Annotations[constants.DEFAULT_KCL_OCI_MANIFEST_SUM] = dependency.Sum
newManifestJSON, err = json.Marshal(newManifest)
assert.NilError(t, err, "Failed to marshal reverted manifest to JSON")

// Compare the new manifest data with the expected manifest data.
assert.Equal(t, string(newManifestJSON), string(originalManifestJSON), "New manifest data mismatch")

// Clean the environment after all tests have been run
err = mock.CleanTestEnv()
assert.NilError(t, err)
}
8 changes: 8 additions & 0 deletions Integrate-Checksum/test_data/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_data"
edition = "v0.9.0"
version = "0.0.1"

[dependencies]
k8s = "1.31"

5 changes: 5 additions & 0 deletions Integrate-Checksum/test_data/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.31"
version = "1.31"
1 change: 1 addition & 0 deletions Integrate-Checksum/test_data/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
23 changes: 23 additions & 0 deletions mock/oci_env_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mock

import (
"os/exec"
)

// StartDockerRegistry starts a local Docker registry by executing a shell script.
func StartDockerRegistry() error {
cmd := exec.Command("../scripts/reg.sh")
return cmd.Run()
}

// PushTestPkgToRegistry pushes the test package to the local Docker registry.
func PushTestPkgToRegistry() error {
cmd := exec.Command("../mock/test_script/push_pkg.sh")
return cmd.Run()
}

// CleanTestEnv cleans up the test environment by executing a cleanup script.
func CleanTestEnv() error {
cmd := exec.Command("../mock/test_script/cleanup_test_environment.sh")
return cmd.Run()
}
8 changes: 8 additions & 0 deletions mock/test_data/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_data"
edition = "v0.9.0"
version = "0.0.1"

[dependencies]
k8s = "1.31"

5 changes: 5 additions & 0 deletions mock/test_data/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.31"
version = "1.31"
1 change: 1 addition & 0 deletions mock/test_data/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
20 changes: 20 additions & 0 deletions mock/test_script/cleanup_test_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Determine the directory where this script is located
SCRIPT_DIR="$(dirname "$(realpath "$0")")"

# Stop and remove the Docker container then remove the Docker image
docker stop kcl-registry
docker rm kcl-registry
docker rmi registry

# Delete all data stored in the Docker registry volume
rm -rf /var/lib/registry/*

# Remove the directory that contains Docker authentication and related scripts
current_dir=$(pwd)
rm -rf "$current_dir/scripts/"

# Delete the 'kcl' binary
cd "$SCRIPT_DIR/../../"
rm -rf ./bin/
31 changes: 31 additions & 0 deletions mock/test_script/push_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

# Push the test_data package to the registry
cd "$SCRIPT_DIR/../test_data"
"$current_dir/bin/kcl" mod push oci://$KPM_REG/$KPM_REPO

0 comments on commit cd7fb71

Please sign in to comment.