aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/tests/derivation.cc
blob: 1cab68e06c054476bb4d2f7f291b27e6dba73c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>

#include "derivations.hh"

#include "tests/libstore.hh"

namespace nix {

class DerivationTest : public LibStoreTest
{
};

#define TEST_JSON(NAME, STR, VAL, DRV_NAME, OUTPUT_NAME) \
    TEST_F(DerivationTest, DerivationOutput_ ## NAME ## _to_json) {    \
        using nlohmann::literals::operator "" _json;           \
        ASSERT_EQ(                                             \
            STR ## _json,                                      \
            (DerivationOutput { VAL }).toJSON(                 \
                *store,                                        \
                DRV_NAME,                                      \
                OUTPUT_NAME));                                 \
    }                                                          \
                                                               \
    TEST_F(DerivationTest, DerivationOutput_ ## NAME ## _from_json) {  \
        using nlohmann::literals::operator "" _json;           \
        ASSERT_EQ(                                             \
            DerivationOutput { VAL },                          \
            DerivationOutput::fromJSON(                        \
                *store,                                        \
                DRV_NAME,                                      \
                OUTPUT_NAME,                                   \
                STR ## _json));                                \
    }

TEST_JSON(inputAddressed,
    R"({
        "path": "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-drv-name-output-name"
    })",
    (DerivationOutput::InputAddressed {
        .path = store->parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-drv-name-output-name"),
    }),
    "drv-name", "output-name")

TEST_JSON(caFixed,
    R"({
        "hashAlgo": "r:sha256",
        "hash": "894517c9163c896ec31a2adbd33c0681fd5f45b2c0ef08a64c92a03fb97f390f",
        "path": "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-drv-name-output-name"
    })",
    (DerivationOutput::CAFixed {
        .ca = FixedOutputInfo {
            .hash = {
                .method = FileIngestionMethod::Recursive,
                .hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
            },
            .references = {},
        },
    }),
    "drv-name", "output-name")

TEST_JSON(caFixedText,
    R"({
        "hashAlgo": "text:sha256",
        "hash": "894517c9163c896ec31a2adbd33c0681fd5f45b2c0ef08a64c92a03fb97f390f",
        "path": "/nix/store/6s1zwabh956jvhv4w9xcdb5jiyanyxg1-drv-name-output-name"
    })",
    (DerivationOutput::CAFixed {
        .ca = TextInfo {
            .hash = {
                .hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
            },
            .references = {},
        },
    }),
    "drv-name", "output-name")

TEST_JSON(caFloating,
    R"({
        "hashAlgo": "r:sha256"
    })",
    (DerivationOutput::CAFloating {
        .method = FileIngestionMethod::Recursive,
        .hashType = htSHA256,
    }),
    "drv-name", "output-name")

TEST_JSON(deferred,
    R"({ })",
    DerivationOutput::Deferred { },
    "drv-name", "output-name")

TEST_JSON(impure,
    R"({
        "hashAlgo": "r:sha256",
        "impure": true
    })",
    (DerivationOutput::Impure {
        .method = FileIngestionMethod::Recursive,
        .hashType = htSHA256,
    }),
    "drv-name", "output-name")

#undef TEST_JSON

#define TEST_JSON(NAME, STR, VAL, DRV_NAME)                     \
    TEST_F(DerivationTest, Derivation_ ## NAME ## _to_json) {   \
        using nlohmann::literals::operator "" _json;            \
        ASSERT_EQ(                                              \
            STR ## _json,                                       \
            (Derivation { VAL }).toJSON(*store));               \
    }                                                           \
                                                                \
    TEST_F(DerivationTest, Derivation_ ## NAME ## _from_json) { \
        using nlohmann::literals::operator "" _json;            \
        ASSERT_EQ(                                              \
            Derivation { VAL },                                 \
            Derivation::fromJSON(                               \
                *store,                                         \
                STR ## _json));                                 \
    }

TEST_JSON(simple,
    R"({
      "name": "my-derivation",
      "inputSrcs": [
        "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"
      ],
      "inputDrvs": {
        "/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv": [
          "cat",
          "dog"
        ]
      },
      "system": "wasm-sel4",
      "builder": "foo",
      "args": [
        "bar",
        "baz"
      ],
      "env": {
        "BIG_BAD": "WOLF"
      },
      "outputs": {}
    })",
    ({
        Derivation drv;
        drv.name = "my-derivation";
        drv.inputSrcs = {
            store->parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"),
        };
        drv.inputDrvs = {
            {
                store->parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"),
                {
                    "cat",
                    "dog",
                },
            }
        };
        drv.platform = "wasm-sel4";
        drv.builder = "foo";
        drv.args = {
            "bar",
            "baz",
        };
        drv.env = {
            {
                "BIG_BAD",
                "WOLF",
            },
        };
        drv;
    }),
    "drv-name")

#undef TEST_JSON

}