client rebuilding in progress

This commit is contained in:
2026-03-04 12:27:52 +02:00
parent 2d34ec5634
commit ae9c29de1e
31 changed files with 908 additions and 467 deletions
+220
View File
@@ -0,0 +1,220 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"mstore/app/server"
"mstore/pkg/client"
"mstore/pkg/terms"
"github.com/stretchr/testify/require"
yaml "go.yaml.in/yaml/v4"
)
func TestAccountLife(t *testing.T) {
var srvport int64 = 10250
srvdir := t.TempDir()
srvaddr := fmt.Sprintf("mstore:mstore@127.0.0.1:%d", srvport)
srv, err := server.NewServer()
require.NoError(t, err)
{
err = srv.Configure()
require.NoError(t, err)
useTmp := true
if useTmp {
srv.SetDatadir(srvdir)
srv.SetLogdir(srvdir)
srv.SetRundir(srvdir)
}
srv.SetPort(srvport)
err = srv.Build()
require.NoError(t, err)
var svcWG sync.WaitGroup
errPipe := make(chan error, 5)
startFunc := func() {
err := srv.Service().Run()
errPipe <- err
svcWG.Done()
}
stopFunc := func() {
srv.Service().Stop()
svcWG.Wait()
err = <-errPipe
require.NoError(t, err)
}
defer stopFunc()
svcWG.Add(1)
go startFunc()
time.Sleep(1 * time.Second)
}
{
// ServiceHello
fmt.Printf("=== ServiceHello ===\n")
cli := client.NewClient(true)
ctx := context.Background()
helloRes, err := cli.ServiceHello(ctx, srvaddr+"/hello", 1*time.Second)
require.NoError(t, err)
require.True(t, helloRes)
}
username := "testuser"
password := "testpass"
var accountID string
{
// CreateAccount
fmt.Printf("=== CreateAccount ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
accountID, err = cli.CreateAccount(ctx, srvaddr, username, password)
require.NoError(t, err)
}
var grantID string
{
// CreateGrant
fmt.Printf("=== CreateGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
grantID, err = cli.CreateGrantByAccountID(ctx, srvaddr, accountID, terms.RightReadAccounts, ".*")
require.NoError(t, err)
require.Equal(t, len(grantID), 36)
}
{
// UpdateGrant
fmt.Printf("=== UpdateGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err := cli.UpdateGrant(ctx, srvaddr, grantID, ".*")
require.NoError(t, err)
}
{
// CreateGrant
fmt.Printf("=== CreateGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
grantID, err = cli.CreateGrantByAccountID(ctx, srvaddr, accountID, terms.RightWriteAccounts, ".*")
require.NoError(t, err)
require.Equal(t, len(grantID), 36)
fmt.Printf("grantID: %s\n", grantID)
}
{
// GetGrant
fmt.Printf("=== GetGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
grantDescr, err := cli.GetGrant(ctx, srvaddr, grantID)
require.NoError(t, err)
require.Equal(t, len(grantID), 36)
fmt.Printf("grantID: %s\n", grantID)
grantYAML, err := yaml.Marshal(grantDescr)
require.NoError(t, err)
fmt.Printf("account:\n%s\n", string(grantYAML))
}
{
// UpdateGrant
fmt.Printf("=== UpdateGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err := cli.UpdateGrant(ctx, srvaddr, grantID, "**")
require.NoError(t, err)
}
{
// GetGrant
fmt.Printf("=== GetGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
grantDescr, err := cli.GetGrant(ctx, srvaddr, grantID)
require.NoError(t, err)
require.Equal(t, len(grantID), 36)
fmt.Printf("grantID: %s\n", grantID)
grantYAML, err := yaml.Marshal(grantDescr)
require.NoError(t, err)
fmt.Printf("account:\n%s\n", string(grantYAML))
}
{
// DeleteGrant
fmt.Printf("=== DeleteGrant ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err := cli.DeleteGrant(ctx, srvaddr, grantID)
require.NoError(t, err)
}
{
// GetAccount
fmt.Printf("=== GetAccount ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
accountDescr, err := cli.GetAccountByID(ctx, srvaddr, accountID)
require.NoError(t, err)
accountYAML, err := yaml.Marshal(accountDescr)
require.NoError(t, err)
fmt.Printf("account:\n%s\n", string(accountYAML))
}
{
// ListAccounts
fmt.Printf("=== ListAccounts ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
accountDescrs, err := cli.ListAccounts(ctx, srvaddr+"/")
require.NoError(t, err)
require.NotZero(t, len(accountDescrs))
nameList := make([]string, 0)
for _, item := range accountDescrs {
nameList = append(nameList, item.Username)
}
accountsYAML, err := yaml.Marshal(nameList)
fmt.Printf("accounts:\n%s\n", string(accountsYAML))
}
{
// DeleteAccount
fmt.Printf("=== DeleteAccount ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err = cli.DeleteAccountByID(ctx, srvaddr, accountID)
require.NoError(t, err)
}
}
+163
View File
@@ -0,0 +1,163 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package test
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"sync"
"testing"
"time"
"mstore/app/server"
"mstore/pkg/client"
"mstore/pkg/terms"
"github.com/stretchr/testify/require"
)
func TestFileLife(t *testing.T) {
var srvport int64 = 10250
srvdir := t.TempDir()
srvaddr := fmt.Sprintf("mstore:mstore@127.0.0.1:%d", srvport)
srv, err := server.NewServer()
require.NoError(t, err)
{
err = srv.Configure()
require.NoError(t, err)
useTmpDir := true
if useTmpDir {
srv.SetDatadir(srvdir)
srv.SetLogdir(srvdir)
srv.SetRundir(srvdir)
}
srv.SetPort(srvport)
err = srv.Build()
require.NoError(t, err)
var svcWG sync.WaitGroup
errPipe := make(chan error, 5)
startFunc := func() {
err := srv.Service().Run()
errPipe <- err
svcWG.Done()
}
stopFunc := func() {
srv.Service().Stop()
svcWG.Wait()
err = <-errPipe
require.NoError(t, err)
}
defer stopFunc()
svcWG.Add(1)
go startFunc()
time.Sleep(1 * time.Second)
}
{
// ServiceHello
fmt.Printf("=== ServiceHello ===\n")
cli := client.NewClient(true)
ctx := context.Background()
helloRes, err := cli.ServiceHello(ctx, srvaddr+"/hello", 1*time.Second)
require.NoError(t, err)
require.True(t, helloRes)
}
filesize := 32
{
// PutFile
tmpdir := t.TempDir()
tmpfile := filepath.Join(tmpdir, "foo.bin")
filedata := make([]byte, filesize)
_, err = rand.Read(filedata)
require.NoError(t, err)
err := os.WriteFile(tmpfile, filedata, 0666)
require.NoError(t, err)
fmt.Printf("=== PutFile ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err = cli.PutFile(ctx, tmpfile, srvaddr+"/foo.bin")
require.NoError(t, err)
}
{
// FileInfo
fmt.Printf("=== FileInfo ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
exists, file, err := cli.FileInfo(ctx, srvaddr+"/foo.bin")
require.NoError(t, err)
require.True(t, exists)
require.NotNil(t, file)
}
{
// GetFile
fmt.Printf("=== GetFile ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
tmpdir := t.TempDir()
tmpfile := filepath.Join(tmpdir, "foo.bin")
recsize, err := cli.GetFile(ctx, srvaddr+"/foo.bin", tmpfile)
require.NoError(t, err)
require.Equal(t, int64(filesize), recsize)
}
{
// ListFiles
fmt.Printf("=== ListFiles ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
files, err := cli.ListFiles(ctx, srvaddr+"/", terms.AsFinePath)
require.NoError(t, err)
require.NotZero(t, len(files))
}
{
// DeleteFile
fmt.Printf("=== DeleteFile ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
err = cli.DeleteFile(ctx, srvaddr+"/foo.bin")
require.NoError(t, err)
}
{
// !FileInfo
fmt.Printf("=== FileInfo ===\n")
cli := client.NewClient(true)
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
exists, _, err := cli.FileInfo(ctx, srvaddr+"/foo.bin")
require.NoError(t, err)
require.False(t, exists)
}
}
+106
View File
@@ -0,0 +1,106 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"mstore/app/server"
"mstore/pkg/client"
"github.com/stretchr/testify/require"
yaml "go.yaml.in/yaml/v4"
)
func TestImageLife(t *testing.T) {
var srvport int64 = 10250
srvdir := t.TempDir()
srvaddr := fmt.Sprintf("mstore:mstore@127.0.0.1:%d", srvport)
srv, err := server.NewServer()
require.NoError(t, err)
{
err = srv.Configure()
require.NoError(t, err)
useTmp := true
if useTmp {
srv.SetDatadir(srvdir)
srv.SetLogdir(srvdir)
srv.SetRundir(srvdir)
}
srv.SetPort(srvport)
err = srv.Build()
require.NoError(t, err)
var svcWG sync.WaitGroup
errPipe := make(chan error, 5)
startFunc := func() {
err := srv.Service().Run()
errPipe <- err
svcWG.Done()
}
stopFunc := func() {
time.Sleep(5 * time.Second)
srv.Service().Stop()
svcWG.Wait()
err = <-errPipe
require.NoError(t, err)
}
defer stopFunc()
svcWG.Add(1)
go startFunc()
time.Sleep(1 * time.Second)
}
{
// ServiceHello
fmt.Printf("=== ServiceHello ===\n")
cli := client.NewClient(true)
ctx := context.Background()
helloRes, err := cli.ServiceHello(ctx, srvaddr+"/hello", 1*time.Second)
require.NoError(t, err)
require.True(t, helloRes)
}
{
// PishImage
fmt.Printf("=== PushImage ===\n")
cli := client.NewClient(true)
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
err := cli.PushImage(ctx, "test-oci.tar", srvaddr+"/foo/test:123")
require.NoError(t, err)
}
{
// ImageAmnifest
fmt.Printf("=== ImageManifest ===\n")
cli := client.NewClient(true)
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
info, err := cli.ImageManifest(ctx, srvaddr+"/foo/test:123")
require.NoError(t, err)
infoYaml, err := yaml.Marshal(info)
require.NoError(t, err)
fmt.Printf("imagemanifest:\n%s\n", string(infoYaml))
}
{
// DeleteImage
fmt.Printf("=== DeleteImage ===\n")
cli := client.NewClient(true)
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
err := cli.DeleteImage(ctx, srvaddr+"/foo/test:123")
require.NoError(t, err)
}
}