kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/oci/oci_test.go (about)

     1  package oci
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"kcl-lang.io/kpm/pkg/settings"
    11  	"kcl-lang.io/kpm/pkg/utils"
    12  )
    13  
    14  const testDataDir = "test_data"
    15  
    16  func getTestDir(subDir string) string {
    17  	pwd, _ := os.Getwd()
    18  	testDir := filepath.Join(pwd, testDataDir)
    19  	testDir = filepath.Join(testDir, subDir)
    20  
    21  	return testDir
    22  }
    23  
    24  func TestLogin(t *testing.T) {
    25  	testPath := getTestDir("test_login")
    26  	testConfPath := filepath.Join(testPath, "config.json")
    27  
    28  	// clean the test dir
    29  	if utils.DirExists(testConfPath) {
    30  		os.Remove(testConfPath)
    31  	}
    32  
    33  	settings := settings.Settings{
    34  		CredentialsFile: testConfPath,
    35  	}
    36  
    37  	hostName := "ghcr.io"
    38  	userName := "invalid_username"
    39  	userPwd := "invalid_password"
    40  
    41  	err := Login(hostName, userName, userPwd, &settings)
    42  	assert.Equal(t, err.Error(), "failed to login 'ghcr.io', please check registry, username and password is valid\nGet \"https://ghcr.io/v2/\": denied: denied\n")
    43  }
    44  
    45  func TestPull(t *testing.T) {
    46  	type TestCase struct {
    47  		Registry string
    48  		Image    string
    49  		Tag      string
    50  	}
    51  
    52  	testCases := []TestCase{
    53  		{"ghcr.io", "kusionstack/opsrule", "0.0.9"},
    54  		{"ghcr.io", "kcl-lang/helloworld", "0.1.1"},
    55  	}
    56  
    57  	defer func() {
    58  		err := os.RemoveAll(getTestDir("test_pull"))
    59  		assert.Equal(t, err, nil)
    60  	}()
    61  
    62  	for _, tc := range testCases {
    63  		client, err := NewOciClient(tc.Registry, tc.Image, settings.GetSettings())
    64  		if err != nil {
    65  			t.Fatalf(err.Error())
    66  		}
    67  
    68  		tmpPath := filepath.Join(getTestDir("test_pull"), tc.Tag)
    69  
    70  		err = os.MkdirAll(tmpPath, 0755)
    71  		assert.Equal(t, err, nil)
    72  
    73  		err = client.Pull(tmpPath, tc.Tag)
    74  		if err != nil {
    75  			t.Errorf(err.Error())
    76  		}
    77  	}
    78  }