package task

import (
	"context"
	"testing"
	"text/template"

	vmoperatorv1beta1 "github.com/VictoriaMetrics/operator/api/operator/v1beta1"
	"github.com/stretchr/testify/assert"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"

	agentv2 "github.com/tidbcloud/observability/agent-control-server/api/v2"
	"github.com/tidbcloud/observability/agent-control-server/pkg/config"
	"github.com/tidbcloud/observability/agent-control-server/pkg/consts"
	"github.com/tidbcloud/observability/agent-control-server/pkg/reconciler"
	"github.com/tidbcloud/observability/agent-control-server/pkg/testkits"
)

func makeVMPodScrapeTask(agent *agentv2.TiDBGroupAgent, opts testkits.TaskOptions) *VMPodScrapeTask {
	objs := []client.Object{agent}
	objs = append(objs, opts.LocalObjects...)
	ctx := &reconciler.Context[*agentv2.TiDBGroupAgent]{
		Agent:     agent,
		Local:     testkits.BuildClient(objs...),
		Remote:    testkits.NewClientGetterWithOpts(opts),
		Config:    &opts.Config,
		Templates: opts.Templates,
	}
	return NewVMPodScrapeTask(ctx)
}

func tidbVMPodScrapeTmpl() *template.Template {
	tmpl, err := template.New("tidb").Parse(`
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMPodScrape
metadata:
  name: {{.Name}}
  namespace: {{.Namespace}}
  labels:
    scrape-by: {{.LabelScrapeBy}}
spec:
  podMetricsEndpoints:
    - port: metrics
      params:
        tenant_id:
          - {{.TenantID}}
        cluster_id:
          - {{.ClusterID}}
        vm_account_id:
          - {{.VMAccountID}}
        vm_project_id:
          - {{.VMProjectID}}
  selector:
    matchLabels:
      tidb-group: {{.TiDBGroup}}
`)
	if err != nil {
		panic(err)
	}
	return tmpl
}

func tiproxyVMPodScrapeTmpl() *template.Template {
	tmpl, err := template.New("tiproxy").Parse(`
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMPodScrape
metadata:
  name: {{.Name}}
  namespace: {{.Namespace}}
  labels:
    scrape-by: {{.LabelScrapeBy}}
spec:
  podMetricsEndpoints:
    - port: metrics
      params:
        tenant_id:
          - {{.TenantID}}
        cluster_id:
          - {{.ClusterID}}
        vm_account_id:
          - {{.VMAccountID}}
        vm_project_id:
          - {{.VMProjectID}}
  selector:
    matchLabels:
      cluster-id: {{.ClusterID}}
`)
	if err != nil {
		panic(err)
	}
	return tmpl
}

func cassandraProxyVMPodScrapeTmpl() *template.Template {
	tmpl, err := template.New("cassandra-proxy").Parse(`
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMPodScrape
metadata:
  name: {{.Name}}
  namespace: {{.Namespace}}
  labels:
    scrape-by: {{.LabelScrapeBy}}
spec:
  podMetricsEndpoints:
    - port: metrics
      params:
        tenant_id:
          - {{.TenantID}}
        cluster_id:
          - {{.ClusterID}}
        vm_account_id:
          - {{.VMAccountID}}
        vm_project_id:
          - {{.VMProjectID}}
  selector:
    matchLabels:
      cluster-id: {{.ClusterID}}
`)
	if err != nil {
		panic(err)
	}
	return tmpl
}

func nodeExporterVMPodScrapeTmpl() *template.Template {
	tmpl, err := template.New("node-exporter").Parse(`
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMPodScrape
metadata:
  name: {{.Name}}
  namespace: {{.Namespace}}
  labels:
    scrape-by: {{.LabelScrapeBy}}
spec:
  podMetricsEndpoints:
    - port: metrics
      params:
        tenant_id:
          - {{.TenantID}}
        cluster_id:
          - {{.ClusterID}}
        vm_account_id:
          - {{.VMAccountID}}
        vm_project_id:
          - {{.VMProjectID}}
  selector: {}
`)
	if err != nil {
		panic(err)
	}
	return tmpl
}

func allVMPodScrapeTmpls() config.Templates {
	return config.Templates{
		"tidbgroup/jobs/tidb.yaml":            tidbVMPodScrapeTmpl(),
		"tidbgroup/jobs/tiproxy.yaml":         tiproxyVMPodScrapeTmpl(),
		"tidbgroup/jobs/cassandra-proxy.yaml": cassandraProxyVMPodScrapeTmpl(),
		"tidbgroup/jobs/node-exporter.yaml":   nodeExporterVMPodScrapeTmpl(),
	}
}

func operationalVMPodScrape(name, namespace string) *vmoperatorv1beta1.VMPodScrape {
	return &vmoperatorv1beta1.VMPodScrape{
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: namespace,
		},
		Status: vmoperatorv1beta1.ScrapeObjectStatus{
			StatusMetadata: vmoperatorv1beta1.StatusMetadata{
				UpdateStatus: vmoperatorv1beta1.UpdateStatusOperational,
			},
		},
	}
}

func basicVMPodScrape(name, namespace string) *vmoperatorv1beta1.VMPodScrape {
	return &vmoperatorv1beta1.VMPodScrape{
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: namespace,
		},
	}
}

func allVMPodScrapeObjects(agent *agentv2.TiDBGroupAgent, namespace string) []client.Object {
	task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
	scrapes := task.vmPodScrapeSpecs()
	objs := make([]client.Object, 0, len(scrapes))
	for _, spec := range scrapes {
		objs = append(objs, basicVMPodScrape(spec.name, namespace))
	}
	return objs
}

func expandingVMPodScrape(name, namespace string) *vmoperatorv1beta1.VMPodScrape {
	return &vmoperatorv1beta1.VMPodScrape{
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: namespace,
		},
		Status: vmoperatorv1beta1.ScrapeObjectStatus{
			StatusMetadata: vmoperatorv1beta1.StatusMetadata{
				UpdateStatus: vmoperatorv1beta1.UpdateStatusExpanding,
			},
		},
	}
}

func TestVMPodScrapeTask(t *testing.T) {
	tests := []struct {
		name   string
		verify func(t *testing.T)
	}{
		{
			name: "Name",
			verify: func(t *testing.T) {
				task := &VMPodScrapeTask{}
				assert.Equal(t, "VMPodScrape", task.Name())
			},
		},
		{
			name: "ClusterMeta",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				meta := task.ClusterMeta()

				assert.Equal(t, consts.AgentTidbGroup, meta.Agent)
				assert.Equal(t, "test-cluster", meta.K8sClusterName)
				assert.Equal(t, "tenant-1", meta.TenantID)
				assert.Equal(t, "app-1", meta.ApplicationID)
				assert.Equal(t, "tidbcloud", meta.BizType)
			},
		},
		{
			name: "vmPodScrapeSpecs without node-exporter annotation",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				specs := task.vmPodScrapeSpecs()

				assert.Equal(t, []vmPodScrapeSpec{
					{tmplKey: "tidbgroup/jobs/tidb.yaml", name: "db-group-123-tidb"},
					{tmplKey: "tidbgroup/jobs/tiproxy.yaml", name: "db-cluster-1-tiproxy"},
					{tmplKey: "tidbgroup/jobs/cassandra-proxy.yaml", name: "db-cluster-1-cassandra-proxy"},
				}, specs)
			},
		},
		{
			name: "vmPodScrapeSpecs with node-exporter annotation",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().
					WithAnnotations(map[string]string{
						consts.AnnotationKeyO11yTiDBNodeExporterScrape: "true",
					}).Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				specs := task.vmPodScrapeSpecs()

				assert.Equal(t, []vmPodScrapeSpec{
					{tmplKey: "tidbgroup/jobs/tidb.yaml", name: "db-group-123-tidb"},
					{tmplKey: "tidbgroup/jobs/node-exporter.yaml", name: "db-cluster-1-node-exporter"},
					{tmplKey: "tidbgroup/jobs/tiproxy.yaml", name: "db-cluster-1-tiproxy"},
					{tmplKey: "tidbgroup/jobs/cassandra-proxy.yaml", name: "db-cluster-1-cassandra-proxy"},
				}, specs)
			},
		},
		{
			name: "vmPodScrapeSpecs skips tiproxy when sharedpool annotation present",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().
					WithAnnotations(map[string]string{
						consts.AnnotationKeyO11yTiProxyDeployType: "shared_tiproxy",
					}).
					Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				specs := task.vmPodScrapeSpecs()

				assert.Equal(t, []vmPodScrapeSpec{
					{tmplKey: "tidbgroup/jobs/tidb.yaml", name: "db-group-123-tidb"},
					{tmplKey: "tidbgroup/jobs/cassandra-proxy.yaml", name: "db-cluster-1-cassandra-proxy"},
				}, specs)
			},
		},
		{
			name: "buildNamespace",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				assert.Equal(t, "tidbpool-456", task.buildNamespace())
			},
		},
		{
			name: "buildVMPodScrape",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().
					WithAnnotations(map[string]string{
						consts.AnnotationKeyO11yVMAccountID: "vm-acc-1",
						consts.AnnotationKeyO11yVMProjectID: "vm-proj-1",
					}).
					Build()

				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{
					Templates: allVMPodScrapeTmpls(),
				})

				for _, spec := range task.vmPodScrapeSpecs() {
					scrape, err := task.buildVMPodScrape(spec)
					assert.NoError(t, err)
					assert.NotNil(t, scrape)
					assert.Equal(t, spec.name, scrape.Name)
					assert.Equal(t, "tidbpool-456", scrape.Namespace)
				}
			},
		},
		{
			name: "LabelScrapeBy",
			verify: func(t *testing.T) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{
					Templates: allVMPodScrapeTmpls(),
				})

				specs := task.vmPodScrapeSpecs()
				scrape, err := task.buildVMPodScrape(specs[0])
				assert.NoError(t, err)
				assert.Equal(t, "tidbpool-456-o11y-vmagent", scrape.Labels["scrape-by"])
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.verify(t)
		})
	}
}

func TestVMPodScrapeTask_Create(t *testing.T) {
	tests := []struct {
		name    string
		agent   *agentv2.TiDBGroupAgent
		opts    testkits.TaskOptions
		wantErr bool
		verify  func(t *testing.T, opts testkits.TaskOptions)
	}{
		{
			name: "create fails with remote client error",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yVMAccountID: "vm-acc-1",
				consts.AnnotationKeyO11yVMProjectID: "vm-proj-1",
			}).Build(),
			opts: testkits.TaskOptions{
				ClientError: true,
				Templates:   allVMPodScrapeTmpls(),
			},
			wantErr: true,
		},
		{
			name: "create succeeds with valid remote client",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yVMAccountID: "vm-acc-1",
				consts.AnnotationKeyO11yVMProjectID: "vm-proj-1",
			}).Build(),
			opts: testkits.TaskOptions{
				Client:    testkits.BuildClient(),
				Templates: allVMPodScrapeTmpls(),
			},
			wantErr: false,
			verify: func(t *testing.T, opts testkits.TaskOptions) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				for _, spec := range task.vmPodScrapeSpecs() {
					scrape := &vmoperatorv1beta1.VMPodScrape{}
					err := opts.Client.Get(context.Background(), types.NamespacedName{
						Name:      spec.name,
						Namespace: "tidbpool-456",
					}, scrape)
					assert.NoError(t, err)
				}
			},
		},
		{
			name: "create succeeds without tiproxy when sharedpool annotation present",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yVMAccountID:       "vm-acc-1",
				consts.AnnotationKeyO11yVMProjectID:       "vm-proj-1",
				consts.AnnotationKeyO11yTiProxyDeployType: "shared_tiproxy",
			}).Build(),
			opts: testkits.TaskOptions{
				Client:    testkits.BuildClient(),
				Templates: allVMPodScrapeTmpls(),
			},
			wantErr: false,
			verify: func(t *testing.T, opts testkits.TaskOptions) {
				agent := testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
					consts.AnnotationKeyO11yTiProxyDeployType: "shared_tiproxy",
				}).Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})

				// tidb and cassandra-proxy should exist
				for _, spec := range task.vmPodScrapeSpecs() {
					scrape := &vmoperatorv1beta1.VMPodScrape{}
					err := opts.Client.Get(context.Background(), types.NamespacedName{
						Name:      spec.name,
						Namespace: "tidbpool-456",
					}, scrape)
					assert.NoError(t, err)
				}

				// tiproxy should NOT exist
				tiproxyScrape := &vmoperatorv1beta1.VMPodScrape{}
				err := opts.Client.Get(context.Background(), types.NamespacedName{
					Name:      "db-cluster-1-tiproxy",
					Namespace: "tidbpool-456",
				}, tiproxyScrape)
				assert.Error(t, err)
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			task := makeVMPodScrapeTask(tt.agent, tt.opts)
			result := task.Create(context.Background())
			if tt.wantErr {
				assert.Error(t, result.Error())
			} else {
				assert.NoError(t, result.Error())
			}
			if tt.verify != nil {
				tt.verify(t, tt.opts)
			}
		})
	}
}

func TestVMPodScrapeTask_PostCreate(t *testing.T) {
	tests := []struct {
		name    string
		agent   *agentv2.TiDBGroupAgent
		opts    testkits.TaskOptions
		wantErr bool
	}{
		{
			name:    "post create fails with remote client error",
			agent:   testkits.NewTiDBGroupAgentBuilder().Build(),
			opts:    testkits.TaskOptions{ClientError: true},
			wantErr: true,
		},
		{
			name:  "post create fails when vmpodscrape not ready",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(),
			},
			wantErr: true,
		},
		{
			name:  "post create succeeds when all vmpodscrapes ready",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					operationalVMPodScrape("db-group-123-tidb", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-tiproxy", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-cassandra-proxy", "tidbpool-456"),
				),
			},
			wantErr: false,
		},
		{
			name:  "post create fails when tidb vmpodscrape not operational",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					expandingVMPodScrape("db-group-123-tidb", "tidbpool-456"),
				),
			},
			wantErr: true,
		},
		{
			name:  "post create fails when tiproxy vmpodscrape not operational",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					operationalVMPodScrape("db-group-123-tidb", "tidbpool-456"),
					expandingVMPodScrape("db-cluster-1-tiproxy", "tidbpool-456"),
				),
			},
			wantErr: true,
		},
		{
			name:  "post create fails when cassandra-proxy vmpodscrape not operational",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					operationalVMPodScrape("db-group-123-tidb", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-tiproxy", "tidbpool-456"),
					expandingVMPodScrape("db-cluster-1-cassandra-proxy", "tidbpool-456"),
				),
			},
			wantErr: true,
		},
		{
			name: "post create fails when node-exporter vmpodscrape not operational",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yTiDBNodeExporterScrape: "true",
			}).Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					operationalVMPodScrape("db-group-123-tidb", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-tiproxy", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-cassandra-proxy", "tidbpool-456"),
					expandingVMPodScrape("db-cluster-1-node-exporter", "tidbpool-456"),
				),
			},
			wantErr: true,
		},
		{
			name: "post create succeeds without tiproxy when sharedpool annotation present",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yTiProxyDeployType: "shared_tiproxy",
			}).Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					operationalVMPodScrape("db-group-123-tidb", "tidbpool-456"),
					operationalVMPodScrape("db-cluster-1-cassandra-proxy", "tidbpool-456"),
				),
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			task := makeVMPodScrapeTask(tt.agent, tt.opts)
			result := task.PostCreate(context.Background())
			if tt.wantErr {
				assert.Error(t, result.Error())
			} else {
				assert.NoError(t, result.Error())
			}
		})
	}
}

func TestVMPodScrapeTask_Delete(t *testing.T) {
	tests := []struct {
		name    string
		agent   *agentv2.TiDBGroupAgent
		opts    testkits.TaskOptions
		wantErr bool
		verify  func(t *testing.T, opts testkits.TaskOptions)
	}{
		{
			name:    "delete fails with remote client error",
			agent:   testkits.NewTiDBGroupAgentBuilder().Build(),
			opts:    testkits.TaskOptions{ClientError: true},
			wantErr: true,
		},
		{
			name:  "delete succeeds with valid remote client",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(),
			},
			wantErr: false,
		},
		{
			name:  "delete succeeds when vmpodscrapes exist",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					allVMPodScrapeObjects(testkits.NewTiDBGroupAgentBuilder().Build(), "tidbpool-456")...,
				),
			},
			wantErr: false,
			verify: func(t *testing.T, opts testkits.TaskOptions) {
				agent := testkits.NewTiDBGroupAgentBuilder().Build()
				task := makeVMPodScrapeTask(agent, testkits.TaskOptions{})
				for _, spec := range task.vmPodScrapeSpecs() {
					scrape := &vmoperatorv1beta1.VMPodScrape{}
					err := opts.Client.Get(context.Background(), types.NamespacedName{
						Name:      spec.name,
						Namespace: "tidbpool-456",
					}, scrape)
					assert.Error(t, err)
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			task := makeVMPodScrapeTask(tt.agent, tt.opts)
			result := task.Delete(context.Background())
			if tt.wantErr {
				assert.Error(t, result.Error())
			} else {
				assert.NoError(t, result.Error())
			}
			if tt.verify != nil {
				tt.verify(t, tt.opts)
			}
		})
	}
}

func TestVMPodScrapeTask_PostDelete(t *testing.T) {
	tests := []struct {
		name    string
		agent   *agentv2.TiDBGroupAgent
		opts    testkits.TaskOptions
		wantErr bool
	}{
		{
			name:    "post delete fails with remote client error",
			agent:   testkits.NewTiDBGroupAgentBuilder().Build(),
			opts:    testkits.TaskOptions{ClientError: true},
			wantErr: true,
		},
		{
			name:  "post delete succeeds when vmpodscrapes deleted",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(),
			},
			wantErr: false,
		},
		{
			name:  "post delete fails when tidb vmpodscrape still exists",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					&vmoperatorv1beta1.VMPodScrape{
						ObjectMeta: metav1.ObjectMeta{
							Name:      "db-group-123-tidb",
							Namespace: "tidbpool-456",
						},
					},
				),
			},
			wantErr: true,
		},
		{
			name:  "post delete fails when tiproxy vmpodscrape still exists",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					&vmoperatorv1beta1.VMPodScrape{
						ObjectMeta: metav1.ObjectMeta{
							Name:      "db-cluster-1-tiproxy",
							Namespace: "tidbpool-456",
						},
					},
				),
			},
			wantErr: true,
		},
		{
			name:  "post delete fails when cassandra-proxy vmpodscrape still exists",
			agent: testkits.NewTiDBGroupAgentBuilder().Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					&vmoperatorv1beta1.VMPodScrape{
						ObjectMeta: metav1.ObjectMeta{
							Name:      "db-cluster-1-cassandra-proxy",
							Namespace: "tidbpool-456",
						},
					},
				),
			},
			wantErr: true,
		},
		{
			name: "post delete fails when node-exporter vmpodscrape still exists",
			agent: testkits.NewTiDBGroupAgentBuilder().WithAnnotations(map[string]string{
				consts.AnnotationKeyO11yTiDBNodeExporterScrape: "true",
			}).Build(),
			opts: testkits.TaskOptions{
				Client: testkits.BuildClient(
					&vmoperatorv1beta1.VMPodScrape{
						ObjectMeta: metav1.ObjectMeta{
							Name:      "db-cluster-1-node-exporter",
							Namespace: "tidbpool-456",
						},
					},
				),
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			task := makeVMPodScrapeTask(tt.agent, tt.opts)
			result := task.PostDelete(context.Background())
			if tt.wantErr {
				assert.Error(t, result.Error())
			} else {
				assert.NoError(t, result.Error())
			}
		})
	}
}
