Bundle the config JSON into the tnull binary

The issue currently is that the tnull binary depends on the config.json
file directly, but that would need to be put somewhere in the RTD image
for it to be readable. If we just build it into the binary instead, all
will be well. This config isn't supposed to be mutable inside the RTD
container anyway.

BUG=chromium:1149702
TEST=local runs

Change-Id: If3845ae7b621953fcec1d8322c44bb6d516b75f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/infra/tnull/+/2558974
Reviewed-by: Prathmesh Prabhu <pprabhu@google.com>
Commit-Queue: Sean Abraham <seanabraham@chromium.org>
Tested-by: Sean Abraham <seanabraham@chromium.org>
diff --git a/generate_metadata.sh b/generate_metadata.sh
index 2eb6f12..650e3aa 100755
--- a/generate_metadata.sh
+++ b/generate_metadata.sh
@@ -12,3 +12,15 @@
 ./_generate_proto_descriptors.sh
 echo "Generating metadata from descriptors..."
 lucicfg generate main.star
+
+echo "// Copyright 2020 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package cmd
+
+// This file is written automatically by generate_metadata.sh.
+// DO NOT EDIT.
+
+const configData = \`$(cat metadata/generated/config.cfg)\`" \
+    > src/tnull/cmd/config.go
diff --git a/src/tnull/cmd/config.go b/src/tnull/cmd/config.go
new file mode 100644
index 0000000..3f71c78
--- /dev/null
+++ b/src/tnull/cmd/config.go
@@ -0,0 +1,714 @@
+// Copyright 2020 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package cmd
+
+// This file is written automatically by generate_metadata.sh.
+// DO NOT EDIT.
+
+const configData = `{
+	"remoteTestDrivers": [
+		{
+			"name": "remoteTestDrivers/tnull",
+			"tests": [
+				{
+					"name": "remoteTestDrivers/tnull/tests/dummy-pass",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nThis is a helper test that will succeed. Used to verify that the RTD's basic\nfunctions are working.\n",
+							"purpose": "No-op success test.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								}
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/report-success",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nreport-success: Helper test verifying reporting success status works correctly. Used to\nverify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report success to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"result": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/report-failure",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nreport-failure: Helper test verifying reporting failure status works correctly. Used to\nverify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report failure to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"result": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/success-with-warnings",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nsuccess-with-warnings: Helper test verifying reporting a complex success status works correctly. Used to\nverify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report success to the RTS despite warning-level errors.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"result": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/fail-on-second-err",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nfail-on-second-err: Helper test verifying reporting a complex failure status works correctly. Used to\nverify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report failure to the RTS if some but not all errors are critical.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"result": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/report-skip",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nreport-skip: Helper test verifying reporting skipped status works correctly. Used to\nverify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report skipped test to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"result": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/no-log",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nno-log: Helper test verifying reporting absent logs works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a absent logs to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/simple-log",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nsimple-log: Helper test verifying reporting a simple log works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a a simple log to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/parallel-log",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nparallel-log: Helper test verifying reporting logs in parallel works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a logs in parallel to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/multiline-logs",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nmultiline-logs: Helper test verifying reporting a single-stream multi-line log works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a a single-stream multi-line log to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/two-logs",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\ntwo-logs: Helper test verifying reporting two logs to one request works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a two logs to one request to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/two-requests",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\ntwo-requests: Helper test verifying reporting logs to two requests works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a logs to two requests to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/mixed-logs",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "chromeos-test-platform-team@google.com"
+							},
+							{
+								"mdbGroup": "chromeos-test-platform-team"
+							}
+						],
+						"details": {
+							"doc": "\nmixed-logs: Helper test verifying reporting two types of log to one request works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a two types of log to one request to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"log": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/no-artifact",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\nno-artifact: Helper test verifying reporting absent artifacts works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a absent artifacts to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/simple-artifact",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\nsimple-artifact: Helper test verifying reporting a simple artifact works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a a simple artifact to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/multiline-artifact",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\nmultiline-artifact: Helper test verifying reporting a multi-line artifact works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a a multi-line artifact to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/two-artifacts",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\ntwo-artifacts: Helper test verifying reporting two artifacts to one request works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a two artifacts to one request to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/duped-artifacts",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\nduped-artifacts: Helper test verifying reporting duplicate artifacts works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a duplicate artifacts to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				},
+				{
+					"name": "remoteTestDrivers/tnull/tests/two-requests",
+					"attributes": [
+						{
+							"name": "suite:dummy"
+						}
+					],
+					"informational": {
+						"authors": [
+							{
+								"email": "email_addr@chromium.org"
+							},
+							{
+								"mdbGroup": "team-mdb-group"
+							}
+						],
+						"details": {
+							"doc": "\ntwo-requests: Helper test verifying reporting artifacts to two requests works correctly.\nUsed to verify that the RTD's interface with the RTS is working.\n",
+							"purpose": "report a artifacts to two requests to the RTS.",
+							"steps": {
+								"setup": {
+									"config": {},
+									"result": {
+										"state": "SUCCEEDED"
+									}
+								},
+								"steps": [
+									{
+										"archive": {
+											"commonArgs": {}
+										}
+									}
+								]
+							}
+						}
+					}
+				}
+			]
+		}
+	]
+}`
diff --git a/src/tnull/cmd/constants.go b/src/tnull/cmd/constants.go
index c4e64a2..8ce5626 100644
--- a/src/tnull/cmd/constants.go
+++ b/src/tnull/cmd/constants.go
@@ -4,5 +4,4 @@
 
 package cmd
 
-const SpecRelPath = "trunk/infra/tnull/metadata/generated/config.cfg"
-const TNullName = "remoteTestDrivers/tnull"
+const tNullName = "remoteTestDrivers/tnull"
diff --git a/src/tnull/cmd/execute.go b/src/tnull/cmd/execute.go
index 8b0a8db..45a09b8 100644
--- a/src/tnull/cmd/execute.go
+++ b/src/tnull/cmd/execute.go
@@ -7,11 +7,10 @@
 import (
 	"context"
 	"fmt"
-	"go.chromium.org/luci/common/cli"
 	"io"
 	"io/ioutil"
-	"os"
-	"path/filepath"
+	"strings"
+
 	"tnull/driver"
 
 	"github.com/golang/protobuf/jsonpb"
@@ -20,6 +19,7 @@
 	tnProto "go.chromium.org/chromiumos/config/go/api/test/harness/tnull/v1"
 	metadata "go.chromium.org/chromiumos/config/go/api/test/metadata/v1"
 	rtd "go.chromium.org/chromiumos/config/go/api/test/rtd/v1"
+	"go.chromium.org/luci/common/cli"
 	"go.chromium.org/luci/common/errors"
 )
 
@@ -62,7 +62,7 @@
 		return errors.Reason("no requests in invocation").Err()
 	}
 
-	lookup, err := extractTestsFromSpecification()
+	lookup, err := extractTestsFromSpecification(ctx)
 	if err != nil {
 		return err
 	}
@@ -87,21 +87,17 @@
 	return errs
 }
 
-func extractTestsFromSpecification() (*tnProto.TestMap, error) {
-	p, err := specPath()
-	if err != nil {
-		return nil, errors.Annotate(err, "extracting tests from spec").Err()
-	}
+func extractTestsFromSpecification(ctx context.Context) (*tnProto.TestMap, error) {
 	var s metadata.Specification
-	if err := readJSONPb(p, &s); err != nil {
+	if err := readJSONPb(configData, &s); err != nil {
 		return nil, errors.Annotate(err, "extracting tests from spec").Err()
 	}
 	testMap := &tnProto.TestMap{Lookup: map[string]*tnProto.Steps{}}
 	for _, rtd := range s.RemoteTestDrivers {
-		if rtd.Name != TNullName {
+		if rtd.Name != tNullName {
 			return nil, errors.Reason(
 				"bad specification, RTD was %s, should be %s",
-				rtd.Name, TNullName).Err()
+				rtd.Name, tNullName).Err()
 		}
 		marshaler := jsonpb.Marshaler{}
 		for _, test := range rtd.Tests {
@@ -169,17 +165,11 @@
 	return nil
 }
 
-// readJSONPb reads a JSON string from inFile and unpacks it as a proto.
+// readJSONPb reads a JSON string from a string and unpacks it as a proto.
 // Unexpected fields are ignored.
-func readJSONPb(inFile string, payload proto.Message) error {
-	r, err := os.Open(inFile)
-	if err != nil {
-		return errors.Annotate(err, "read JSON pb").Err()
-	}
-	defer r.Close()
-
+func readJSONPb(s string, payload proto.Message) error {
 	unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: true}
-	if err := unmarshaler.Unmarshal(r, payload); err != nil {
+	if err := unmarshaler.Unmarshal(strings.NewReader(s), payload); err != nil {
 		return errors.Annotate(err, "read JSON pb").Err()
 	}
 	return nil
@@ -195,11 +185,3 @@
 	}
 	return nil
 }
-
-func specPath() (string, error) {
-	if home, err := os.UserHomeDir(); err != nil {
-		return "", err
-	} else {
-		return filepath.Join(home, SpecRelPath), nil
-	}
-}
diff --git a/src/tnull/cmd/generate.go b/src/tnull/cmd/generate.go
index a74be0f..a8a0c0d 100644
--- a/src/tnull/cmd/generate.go
+++ b/src/tnull/cmd/generate.go
@@ -58,7 +58,7 @@
 }
 
 func (g *gen) innerRun(ctx context.Context) error {
-	lookup, err := extractTestsFromSpecification()
+	lookup, err := extractTestsFromSpecification(ctx)
 	if err != nil {
 		return err
 	}
@@ -206,5 +206,5 @@
 }
 
 func (n *NameStruct) inferredName() string {
-	return uri.Join(TNullName, "tests", n.shortName)
+	return uri.Join(tNullName, "tests", n.shortName)
 }