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
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@ import (
"strings"

"github.com/GoogleCloudPlatform/khi/pkg/common/khictx"
pb "github.com/GoogleCloudPlatform/khi/pkg/generated/khifile/v6"
khifilev6 "github.com/GoogleCloudPlatform/khi/pkg/model/khifile/v6"
googlecloudcommon_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/googlecloudcommon/contract"
inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract"
)

// MustComposerEnvironmentTimeline returns the timeline path for a Composer Environment.
func MustComposerEnvironmentTimeline(ctx context.Context, projectID, environmentName string) *khifilev6.TimelinePath {
if projectID == "" {
projectID = "unknown"
}
// MustAirflowTimeline returns the root timeline path for an Airflow environment.
func MustAirflowTimeline(ctx context.Context, environmentName string) *khifilev6.TimelinePath {
if environmentName == "" {
environmentName = "unknown"
}
builder := khictx.MustGetValue(ctx, inspectioncore_contract.Builder)
projectPath := googlecloudcommon_contract.MustGCPProjectTimeline(ctx, projectID)
return builder.TimelineAccumulator.GetPath(projectPath, khifilev6.PathSegment{
return builder.TimelineAccumulator.GetPath(nil, khifilev6.PathSegment{
Name: environmentName,
Type: TimelineTypeComposerEnvironment,
Type: TimelineTypeAirflow,
})
}

Expand Down Expand Up @@ -101,32 +95,24 @@ func MustAirflowComponentsRootTimeline(ctx context.Context, envPath *khifilev6.T
}

// MustAirflowComponentTimeline returns the timeline path for a specific Airflow component.
func MustAirflowComponentTimeline(ctx context.Context, envPath *khifilev6.TimelinePath, componentType *pb.TimelineType, name string) *khifilev6.TimelinePath {
func MustAirflowComponentTimeline(ctx context.Context, envPath *khifilev6.TimelinePath, name string) *khifilev6.TimelinePath {
if name == "" {
name = "unknown"
}
compRoot := MustAirflowComponentsRootTimeline(ctx, envPath)
builder := khictx.MustGetValue(ctx, inspectioncore_contract.Builder)
return builder.TimelineAccumulator.GetPath(compRoot, khifilev6.PathSegment{
Name: name,
Type: componentType,
Type: TimelineTypeAirflowComponent,
})
}

// MustAirflowWorkerTimeline returns the timeline path for an Airflow worker.
func MustAirflowWorkerTimeline(ctx context.Context, envPath *khifilev6.TimelinePath, workerHost string) *khifilev6.TimelinePath {
if workerHost == "" {
workerHost = "unknown"
}
return MustAirflowComponentTimeline(ctx, envPath, TimelineTypeAirflowWorker, workerHost)
}

// MustAirflowDAGProcessorManagerRootTimeline returns the root timeline path for DAG Processor Manager.
func MustAirflowDAGProcessorManagerRootTimeline(ctx context.Context, envPath *khifilev6.TimelinePath) *khifilev6.TimelinePath {
// MustAirflowDAGFilesTimeline returns the root timeline path for DAG files.
func MustAirflowDAGFilesTimeline(ctx context.Context, envPath *khifilev6.TimelinePath) *khifilev6.TimelinePath {
builder := khictx.MustGetValue(ctx, inspectioncore_contract.Builder)
return builder.TimelineAccumulator.GetPath(envPath, khifilev6.PathSegment{
Name: "DAG Processor Manager",
Type: TimelineTypeDAGProcessorManager,
Name: "DAG files",
Type: TimelineTypeDAGFiles,
})
}

Expand All @@ -135,7 +121,7 @@ func MustAirflowDAGFileTimeline(ctx context.Context, envPath *khifilev6.Timeline
if filePath == "" {
filePath = "unknown"
}
dpmRoot := MustAirflowDAGProcessorManagerRootTimeline(ctx, envPath)
dpmRoot := MustAirflowDAGFilesTimeline(ctx, envPath)
builder := khictx.MustGetValue(ctx, inspectioncore_contract.Builder)
trimmedPath := strings.TrimPrefix(filePath, "/home/airflow/gcs/dags/")
if trimmedPath == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,41 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestMustComposerEnvironmentTimeline(t *testing.T) {
func TestMustAirflowTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)

testCases := []struct {
name string
projectID string
environmentName string
wantProject string
wantEnv string
}{
{
name: "valid input",
projectID: "my-project",
environmentName: "my-env",
wantProject: "my-project",
wantEnv: "my-env",
},
{
name: "empty project",
projectID: "",
environmentName: "my-env",
wantProject: "unknown",
wantEnv: "my-env",
},
{
name: "empty env",
projectID: "my-project",
environmentName: "",
wantProject: "my-project",
wantEnv: "unknown",
},
{
name: "both empty",
projectID: "",
environmentName: "",
wantProject: "unknown",
wantEnv: "unknown",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := MustComposerEnvironmentTimeline(ctx, tc.projectID, tc.environmentName)
got := MustAirflowTimeline(ctx, tc.environmentName)
if got == nil {
t.Fatal("expected timeline path to be not nil")
}
if diff := cmp.Diff(tc.wantEnv, got.Name.Resolve()); diff != "" {
t.Errorf("MustComposerEnvironmentTimeline() environmentName mismatch (-want +got):\n%s", diff)
t.Errorf("MustAirflowTimeline() environmentName mismatch (-want +got):\n%s", diff)
}
if got.Parent == nil {
t.Fatal("expected parent timeline path to be not nil")
if got.Parent != nil {
t.Errorf("MustAirflowTimeline() expected root timeline (nil parent), got %v", got.Parent)
}
if diff := cmp.Diff(tc.wantProject, got.Parent.Name.Resolve()); diff != "" {
t.Errorf("MustComposerEnvironmentTimeline() projectID mismatch (-want +got):\n%s", diff)
if diff := cmp.Diff(TimelineTypeAirflow.GetId(), got.Type.GetId()); diff != "" {
t.Errorf("MustAirflowTimeline() type ID mismatch (-want +got):\n%s", diff)
}
})
}
Expand All @@ -86,7 +66,7 @@ func TestMustComposerEnvironmentTimeline(t *testing.T) {
func TestMustAirflowDAGTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")

testCases := []struct {
name string
Expand Down Expand Up @@ -121,7 +101,7 @@ func TestMustAirflowDAGTimeline(t *testing.T) {
func TestMustAirflowDAGRunTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")

testCases := []struct {
name string
Expand Down Expand Up @@ -182,7 +162,7 @@ func TestMustAirflowDAGRunTimeline(t *testing.T) {
func TestMustAirflowTaskInstanceTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")
runPath := MustAirflowDAGRunTimeline(ctx, envPath, "my-dag", "my-run")

testCases := []struct {
Expand Down Expand Up @@ -218,7 +198,7 @@ func TestMustAirflowTaskInstanceTimeline(t *testing.T) {
func TestMustAirflowComponentTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")

testCases := []struct {
name string
Expand All @@ -239,56 +219,24 @@ func TestMustAirflowComponentTimeline(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := MustAirflowComponentTimeline(ctx, envPath, TimelineTypeAirflowScheduler, tc.componentName)
got := MustAirflowComponentTimeline(ctx, envPath, tc.componentName)
if got == nil {
t.Fatal("expected timeline path to be not nil")
}
if diff := cmp.Diff(TimelineTypeAirflowComponent.GetId(), got.Type.GetId()); diff != "" {
t.Errorf("MustAirflowComponentTimeline() timeline type mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tc.wantName, got.Name.Resolve()); diff != "" {
t.Errorf("MustAirflowComponentTimeline() componentName mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestMustAirflowWorkerTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")

testCases := []struct {
name string
workerHost string
wantHost string
}{
{
name: "valid input",
workerHost: "worker-1",
wantHost: "worker-1",
},
{
name: "empty worker host",
workerHost: "",
wantHost: "unknown",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := MustAirflowWorkerTimeline(ctx, envPath, tc.workerHost)
if got == nil {
t.Fatal("expected timeline path to be not nil")
}
if diff := cmp.Diff(tc.wantHost, got.Name.Resolve()); diff != "" {
t.Errorf("MustAirflowWorkerTimeline() workerHost mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestMustAirflowDAGFileTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")

testCases := []struct {
name string
Expand Down Expand Up @@ -333,7 +281,7 @@ func TestMustAirflowDAGFileTimeline(t *testing.T) {
func TestMustAirflowDAGProcessorManagerInstanceTimeline(t *testing.T) {
builder := khifilev6.NewBuilder()
ctx := khictx.WithValue(t.Context(), inspectioncore_contract.Builder, builder)
envPath := MustComposerEnvironmentTimeline(ctx, "my-project", "my-env")
envPath := MustAirflowTimeline(ctx, "my-env")

testCases := []struct {
name string
Expand Down
Loading
Loading