Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions fleetspeak/src/client/daemonservice/daemonservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/google/fleetspeak/fleetspeak/src/client/clitesting"
"github.com/google/fleetspeak/fleetspeak/src/client/service"
"github.com/google/fleetspeak/fleetspeak/src/client/stats"
"github.com/google/fleetspeak/fleetspeak/src/common"
"github.com/google/fleetspeak/fleetspeak/src/common/anypbtest"

dspb "github.com/google/fleetspeak/fleetspeak/src/client/daemonservice/proto/fleetspeak_daemonservice"
Expand Down Expand Up @@ -588,6 +589,59 @@ waitLoop:
}
}

func TestClientIDEnvVar(t *testing.T) {
cid, err := common.StringToClientID("0102030405060708")
if err != nil {
t.Fatalf("Failed to create ClientID: %v", err)
}
sc := clitesting.FakeServiceContext{
OutChan: make(chan *fspb.Message, 100),
LocalInfo: &service.LocalInfo{
ClientID: cid,
},
}

bashCmd := "echo $FLEETSPEAK_CLIENT_ID"

cfg := &fspb.ClientServiceConfig{
Name: "TestDaemonService",
Config: anypbtest.New(t, &dspb.Config{
Argv: []string{"bash", "-c", bashCmd},
StdParams: &dspb.Config_StdParams{
ServiceName: "test_service",
FlushBytes: 1024,
},
}),
}

s, err := Factory(cfg)
if err != nil {
t.Fatalf("Factory(...): %v", err)
}
if err := s.Start(&sc); err != nil {
t.Fatalf("Service.Start(...): %v", err)
}
defer s.Stop()

for {
select {
case <-time.After(time.Second):
t.Fatal("Timeout waiting for StdOutput")
case m := <-sc.OutChan:
if m.MessageType == "StdOutput" {
var out dspb.StdOutputData
if err := m.Data.UnmarshalTo(&out); err != nil {
t.Fatalf("UnmarshalTo failed: %v", err)
}
if got, want := string(out.Stdout), "0102030405060708\n"; got != want {
t.Errorf("Unexpected output: got %q, want %q", got, want)
}
return
}
}
}
}

func init() {
// Hack so that daemonservice logs get written to the right place.
if d := os.Getenv("TEST_UNDECLARED_OUTPUTS_DIR"); d != "" {
Expand Down
4 changes: 4 additions & 0 deletions fleetspeak/src/client/daemonservice/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func New(ctx context.Context, daemonServiceName string, cfg *dspb.Config, sc ser
ret.samplePeriod = time.Duration(cfg.ResourceMonitoringSamplePeriodSeconds) * time.Second
}

if info := sc.GetLocalInfo(); info != nil {
ret.cmd.AddEnvVar("FLEETSPEAK_CLIENT_ID=" + info.ClientID.String())
}

var err error
ret.channel, err = ret.cmd.SetupCommsChannel()
if err != nil {
Expand Down
Loading