diff --git a/kcontext/kcontext.go b/kcontext/kcontext.go index 639818b7c..5295676b1 100644 --- a/kcontext/kcontext.go +++ b/kcontext/kcontext.go @@ -27,19 +27,9 @@ type KContext struct { Stdout io.Writer `msgpack:"-"` Stderr io.Writer `msgpack:"-"` - Username string - Hostname string - CommandLine string - MachineID string KeyFromFile string CacheDir string - OperatingSystem string - Architecture string - ProcessID int - - Client string - CWD string MaxConcurrency int diff --git a/kcontext/kcontext_test.go b/kcontext/kcontext_test.go deleted file mode 100644 index 07bf755bf..000000000 --- a/kcontext/kcontext_test.go +++ /dev/null @@ -1,181 +0,0 @@ -package kcontext - -import ( - "testing" - - "github.com/PlakarKorp/kloset/caching" - "github.com/PlakarKorp/kloset/encryption/keypair" - "github.com/PlakarKorp/kloset/logging" - "github.com/google/uuid" - "github.com/stretchr/testify/require" -) - -func TestContext_SettersAndGetters(t *testing.T) { - ctx := NewKContext() - - defaultLogger := logging.NewLogger(nil, nil) - defaultCachingManager := caching.NewManager("/tmp/test_plakar") - defaultKeyPair, err := keypair.Generate() - require.NoError(t, err) - - tests := []struct { - name string - setter func() - getter func() interface{} - expected interface{} - }{ - { - name: "SetUsername", - setter: func() { - ctx.Username = "testuser" - }, - getter: func() interface{} { return ctx.Username }, - expected: "testuser", - }, - { - name: "SetHostname", - setter: func() { - ctx.Hostname = "testhost" - }, - getter: func() interface{} { return ctx.Hostname }, - expected: "testhost", - }, - { - name: "SetCommandLine", - setter: func() { - ctx.CommandLine = "test command line" - }, - getter: func() interface{} { return ctx.CommandLine }, - expected: "test command line", - }, - { - name: "SetMachineID", - setter: func() { - ctx.MachineID = "machine-123" - }, - getter: func() interface{} { return ctx.MachineID }, - expected: "machine-123", - }, - { - name: "SetKeyFromFile", - setter: func() { - ctx.KeyFromFile = "key123" - }, - getter: func() interface{} { return ctx.KeyFromFile }, - expected: "key123", - }, - { - name: "SetCacheDir", - setter: func() { - ctx.CacheDir = "/cache/testuser" - }, - getter: func() interface{} { return ctx.CacheDir }, - expected: "/cache/testuser", - }, - { - name: "SetOperatingSystem", - setter: func() { - ctx.OperatingSystem = "linux" - }, - getter: func() interface{} { return ctx.OperatingSystem }, - expected: "linux", - }, - { - name: "SetArchitecture", - setter: func() { - ctx.Architecture = "amd64" - }, - getter: func() interface{} { return ctx.Architecture }, - expected: "amd64", - }, - { - name: "SetProcessID", - setter: func() { - ctx.ProcessID = 12345 - }, - getter: func() interface{} { return ctx.ProcessID }, - expected: 12345, - }, - { - name: "SetIdentity", - setter: func() { - ctx.Identity = uuid.MustParse("123e4567-e89b-12d3-a456-426614174000") - }, - getter: func() interface{} { return ctx.Identity }, - expected: uuid.MustParse("123e4567-e89b-12d3-a456-426614174000"), - }, - { - name: "SetKeypair", - setter: func() { - ctx.Keypair = defaultKeyPair - }, - getter: func() interface{} { return ctx.Keypair }, - expected: defaultKeyPair, - }, - { - name: "SetPlakarClient", - setter: func() { - ctx.Client = "plakar-client" - }, - getter: func() interface{} { return ctx.Client }, - expected: "plakar-client", - }, - { - name: "SetMaxConcurrency", - setter: func() { - ctx.MaxConcurrency = 10 - }, - getter: func() interface{} { return ctx.MaxConcurrency }, - expected: 10, - }, - { - name: "SetCache", - setter: func() { - ctx.SetCache(defaultCachingManager) - }, - getter: func() interface{} { return ctx.GetCache() }, - expected: defaultCachingManager, - }, - { - name: "SetLogger", - setter: func() { - ctx.SetLogger(defaultLogger) - }, - getter: func() interface{} { return ctx.GetLogger() }, - expected: defaultLogger, - }, - { - name: "SetCWD", - setter: func() { - ctx.CWD = "/current/working/dir" - }, - getter: func() interface{} { return ctx.CWD }, - expected: "/current/working/dir", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - test.setter() - if result := test.getter(); result != test.expected { - t.Errorf("%s failed: expected %#v, got %#v", test.name, test.expected, result) - } - }) - } -} - -func TestAppContextCloseEvents(t *testing.T) { - ctx := NewKContext() - events := ctx.Events() - if events == nil { - t.Errorf("events is nil") - } - ctx.Close() - // Check if events is closed - select { - case <-events.Listen(): - t.Errorf("events is not closed") - default: - // events is closed - } -} diff --git a/snapshot/builder.go b/snapshot/builder.go index 47bbb4f41..a6b6fb8de 100644 --- a/snapshot/builder.go +++ b/snapshot/builder.go @@ -30,7 +30,18 @@ type Builder struct { Header *header.Header } -func Create(repo *repository.Repository, packingStrategy repository.RepositoryType) (*Builder, error) { +type BuilderOptions struct { + Username string + Hostname string + CommandLine string + MachineID string + OperatingSystem string + Architecture string + Client string + ProcessID int +} + +func Create(repo *repository.Repository, packingStrategy repository.RepositoryType, opts *BuilderOptions) (*Builder, error) { identifier := objects.RandomMAC() scanCache, err := repo.AppContext().GetCache().Scan(identifier) if err != nil { @@ -49,22 +60,22 @@ func Create(repo *repository.Repository, packingStrategy repository.RepositoryTy snap.Header.Identity.Identifier = snap.AppContext().Identity snap.Header.Identity.PublicKey = snap.AppContext().Keypair.PublicKey } - snap.Header.SetContext("Hostname", snap.AppContext().Hostname) - snap.Header.SetContext("Username", snap.AppContext().Username) - snap.Header.SetContext("OperatingSystem", snap.AppContext().OperatingSystem) - snap.Header.SetContext("MachineID", snap.AppContext().MachineID) - snap.Header.SetContext("CommandLine", snap.AppContext().CommandLine) - snap.Header.SetContext("ProcessID", fmt.Sprintf("%d", snap.AppContext().ProcessID)) - snap.Header.SetContext("Architecture", snap.AppContext().Architecture) + snap.Header.SetContext("Hostname", opts.Hostname) + snap.Header.SetContext("Username", opts.Username) + snap.Header.SetContext("OperatingSystem", opts.OperatingSystem) + snap.Header.SetContext("MachineID", opts.MachineID) + snap.Header.SetContext("CommandLine", opts.CommandLine) + snap.Header.SetContext("ProcessID", fmt.Sprintf("%d", opts.ProcessID)) + snap.Header.SetContext("Architecture", opts.Architecture) snap.Header.SetContext("NumCPU", fmt.Sprintf("%d", runtime.NumCPU())) snap.Header.SetContext("MaxProcs", fmt.Sprintf("%d", runtime.GOMAXPROCS(0))) - snap.Header.SetContext("Client", snap.AppContext().Client) + snap.Header.SetContext("Client", opts.Client) repo.Logger().Trace("snapshot", "%x: Create()", snap.Header.GetIndexShortID()) return snap, nil } -func CreateWithRepositoryWriter(repo *repository.RepositoryWriter) (*Builder, error) { +func CreateWithRepositoryWriter(repo *repository.RepositoryWriter, opts *BuilderOptions) (*Builder, error) { identifier := objects.RandomMAC() scanCache, err := repo.AppContext().GetCache().Scan(identifier) if err != nil { @@ -84,16 +95,16 @@ func CreateWithRepositoryWriter(repo *repository.RepositoryWriter) (*Builder, er snap.Header.Identity.PublicKey = snap.AppContext().Keypair.PublicKey } - snap.Header.SetContext("Hostname", snap.AppContext().Hostname) - snap.Header.SetContext("Username", snap.AppContext().Username) - snap.Header.SetContext("OperatingSystem", snap.AppContext().OperatingSystem) - snap.Header.SetContext("MachineID", snap.AppContext().MachineID) - snap.Header.SetContext("CommandLine", snap.AppContext().CommandLine) - snap.Header.SetContext("ProcessID", fmt.Sprintf("%d", snap.AppContext().ProcessID)) - snap.Header.SetContext("Architecture", snap.AppContext().Architecture) + snap.Header.SetContext("Hostname", opts.Hostname) + snap.Header.SetContext("Username", opts.Username) + snap.Header.SetContext("OperatingSystem", opts.OperatingSystem) + snap.Header.SetContext("MachineID", opts.MachineID) + snap.Header.SetContext("CommandLine", opts.CommandLine) + snap.Header.SetContext("ProcessID", fmt.Sprintf("%d", opts.ProcessID)) + snap.Header.SetContext("Architecture", opts.Architecture) snap.Header.SetContext("NumCPU", fmt.Sprintf("%d", runtime.NumCPU())) snap.Header.SetContext("MaxProcs", fmt.Sprintf("%d", runtime.GOMAXPROCS(0))) - snap.Header.SetContext("Client", snap.AppContext().Client) + snap.Header.SetContext("Client", opts.Client) repo.Logger().Trace("snapshot", "%x: Create()", snap.Header.GetIndexShortID()) return snap, nil @@ -132,7 +143,7 @@ func (snap *Builder) Lock() (chan bool, error) { return lockDone, nil } - lock := repository.NewSharedLock(snap.AppContext().Hostname) + lock := repository.NewSharedLock(snap.Header.GetContext("Hostname")) buffer := &bytes.Buffer{} err := lock.SerializeToStream(buffer) @@ -195,7 +206,7 @@ func (snap *Builder) Lock() (chan bool, error) { snap.repository.DeleteLock(snap.Header.Identifier) return case <-time.After(repository.LOCK_REFRESH_RATE): - lock := repository.NewSharedLock(snap.AppContext().Hostname) + lock := repository.NewSharedLock(snap.Header.GetContext("Hostname")) buffer := &bytes.Buffer{} diff --git a/testing/context.go b/testing/context.go index 47619dac5..33a628e21 100644 --- a/testing/context.go +++ b/testing/context.go @@ -27,8 +27,6 @@ func GenerateContext(t *testing.T, bufout *bytes.Buffer, buferr *bytes.Buffer) * ctx := kcontext.NewKContext() - ctx.Client = "plakar-test/1.0.0" - // create a repository ctx.MaxConcurrency = 1 if bufout != nil && buferr != nil { diff --git a/testing/repository.go b/testing/repository.go index 6c7768e19..32475baee 100644 --- a/testing/repository.go +++ b/testing/repository.go @@ -34,10 +34,7 @@ func GenerateRepository(t *testing.T, bufout *bytes.Buffer, buferr *bytes.Buffer ctx := kcontext.NewKContext() - ctx.Client = "plakar-test/1.0.0" - // create a storage - r, err := storage.New(ctx, map[string]string{"location": "mock://" + tmpRepoDir}) require.NotNil(t, r) require.NoError(t, err) diff --git a/testing/snapshot.go b/testing/snapshot.go index 5cd4386c0..11abc6347 100644 --- a/testing/snapshot.go +++ b/testing/snapshot.go @@ -125,7 +125,7 @@ func GenerateSnapshot(t *testing.T, repo *repository.Repository, files []MockFil } // create a snapshot - builder, err := snapshot.Create(repo, repository.DefaultType) + builder, err := snapshot.Create(repo, repository.DefaultType, &snapshot.BuilderOptions{}) require.NoError(t, err) require.NotNil(t, builder)